Challenge - 5 Problems
Matrix Zeroes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of the matrix after running this code?
Given the matrix below, what will be its state after the function
setZeroes runs?DSA Javascript
function setZeroes(matrix) {
const m = matrix.length;
const n = matrix[0].length;
let firstRowZero = false;
let firstColZero = false;
for (let i = 0; i < m; i++) {
if (matrix[i][0] === 0) firstColZero = true;
}
for (let j = 0; j < n; j++) {
if (matrix[0][j] === 0) firstRowZero = true;
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
matrix[i][j] = 0;
}
}
}
if (firstColZero) {
for (let i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
if (firstRowZero) {
for (let j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
}
const matrix = [
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
];
setZeroes(matrix);
console.log(matrix);Attempts:
2 left
💡 Hint
Check how the first row and first column are used to mark zeroes.
✗ Incorrect
The code marks rows and columns to be zeroed using the first row and column as markers. Then it sets the cells to zero accordingly. The zero at position (1,1) causes row 1 and column 1 to be zeroed.
❓ Predict Output
intermediateWhat is the output of the matrix after running this code?
Given the matrix below, what will be its state after the function
setZeroes runs?DSA Javascript
function setZeroes(matrix) {
const m = matrix.length;
const n = matrix[0].length;
let firstRowZero = false;
let firstColZero = false;
for (let i = 0; i < m; i++) {
if (matrix[i][0] === 0) firstColZero = true;
}
for (let j = 0; j < n; j++) {
if (matrix[0][j] === 0) firstRowZero = true;
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
matrix[i][j] = 0;
}
}
}
if (firstColZero) {
for (let i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
if (firstRowZero) {
for (let j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
}
const matrix = [
[0, 1, 2, 0],
[3, 4, 5, 2],
[1, 3, 1, 5]
];
setZeroes(matrix);
console.log(matrix);Attempts:
2 left
💡 Hint
Remember to zero the first row and column if needed after marking.
✗ Incorrect
Zeros in first row and last column cause entire first row and last column to be zeroed. Also, zero in first column causes first column to be zeroed.
🔧 Debug
advancedWhy does this code cause an infinite loop?
Consider this code snippet for setting matrix zeroes. Why does it cause an infinite loop?
DSA Javascript
function setZeroes(matrix) {
const m = matrix.length;
const n = matrix[0].length;
let firstRowZero = false;
let firstColZero = false;
for (let i = 0; i <= m; i++) {
if (matrix[i][0] === 0) firstColZero = true;
}
for (let j = 0; j < n; j++) {
if (matrix[0][j] === 0) firstRowZero = true;
}
// rest of code omitted
}Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully.
✗ Incorrect
The loop runs from i = 0 to i <= m, but valid indices are 0 to m-1. Accessing matrix[m][0] causes error or infinite behavior.
❓ Predict Output
advancedWhat is the output of the matrix after running this code?
Given the matrix below, what will be its state after the function
setZeroes runs?DSA Javascript
function setZeroes(matrix) {
const m = matrix.length;
const n = matrix[0].length;
let firstRowZero = false;
let firstColZero = false;
for (let i = 0; i < m; i++) {
if (matrix[i][0] === 0) firstColZero = true;
}
for (let j = 0; j < n; j++) {
if (matrix[0][j] === 0) firstRowZero = true;
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
matrix[i][j] = 0;
}
}
}
if (firstColZero) {
for (let i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
if (firstRowZero) {
for (let j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
}
const matrix = [
[1, 2, 3],
[4, 0, 6],
[7, 8, 9]
];
setZeroes(matrix);
console.log(matrix);Attempts:
2 left
💡 Hint
Focus on how the zero at (1,1) affects its row and column.
✗ Incorrect
The zero at (1,1) causes row 1 and column 1 to be zeroed. The first row and first column are not zeroed because they have no zero initially.
❓ Predict Output
expertWhat is the output of the matrix after running this code?
Given the matrix below, what will be its state after the function
setZeroes runs?DSA Javascript
function setZeroes(matrix) {
const m = matrix.length;
const n = matrix[0].length;
let firstRowZero = false;
let firstColZero = false;
for (let i = 0; i < m; i++) {
if (matrix[i][0] === 0) firstColZero = true;
}
for (let j = 0; j < n; j++) {
if (matrix[0][j] === 0) firstRowZero = true;
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][j] === 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
matrix[i][j] = 0;
}
}
}
if (firstColZero) {
for (let i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
if (firstRowZero) {
for (let j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
}
const matrix = [
[0, 1, 2, 0],
[3, 4, 0, 2],
[1, 3, 1, 5]
];
setZeroes(matrix);
console.log(matrix);Attempts:
2 left
💡 Hint
Multiple zeros in matrix affect rows and columns to zero.
✗ Incorrect
Zeros at (0,0), (0,3), and (1,2) cause first row, first column, third column, and second row to be zeroed accordingly.
