Challenge - 5 Problems
Matrix Zeroes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Set Matrix Zeroes In Place - Simple Case
What is the state of the matrix after running the
setZeroes method on the given matrix?DSA Java
int[][] matrix = { {1, 1, 1}, {1, 0, 1}, {1, 1, 1} }; // After calling setZeroes(matrix), what is matrix?
Attempts:
2 left
💡 Hint
Remember that if an element is zero, its entire row and column become zero.
✗ Incorrect
The zero at position (1,1) causes the entire row 1 and column 1 to be set to zero. So the middle row and middle column become zero.
❓ Predict Output
intermediateOutput of Set Matrix Zeroes In Place - Multiple Zeroes
What is the matrix after applying
setZeroes on this matrix?DSA Java
int[][] matrix = { {0, 1, 2, 0}, {3, 4, 5, 2}, {1, 3, 1, 5} }; // After setZeroes(matrix), what is matrix?
Attempts:
2 left
💡 Hint
Check all zero positions and mark their rows and columns.
✗ Incorrect
Zeroes at (0,0) and (0,3) cause row 0 and columns 0 and 3 to be zeroed. Rows 1 and 2 have columns 0 and 3 zeroed.
🔧 Debug
advancedIdentify the Bug in This Set Matrix Zeroes Code
What error or problem will this code cause when run on a matrix with zeroes?
DSA Java
public void setZeroes(int[][] matrix) { int rows = matrix.length; int cols = matrix[0].length; boolean[] rowZero = new boolean[rows]; boolean[] colZero = new boolean[cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (matrix[i][j] == 0) { rowZero[i] = true; colZero[j] = true; } } } for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (rowZero[i] || colZero[j]) { matrix[i][j] = 0; } } } }
Attempts:
2 left
💡 Hint
Check what happens if matrix has zero rows or zero columns.
✗ Incorrect
If matrix.length == 0, matrix[0] access causes ArrayIndexOutOfBoundsException. Also if matrix[0].length == 0, cols is zero but code still runs loops.
❓ Predict Output
advancedOutput of Optimized In-Place Set Matrix Zeroes
Given this optimized code using first row and column as markers, what is the matrix after running
setZeroes?DSA Java
int[][] matrix = { {1, 2, 3}, {4, 0, 6}, {7, 8, 9} }; // After setZeroes(matrix), what is matrix?
Attempts:
2 left
💡 Hint
Remember the first row and column mark which rows and columns to zero.
✗ Incorrect
Zero at (1,1) marks row 1 and column 1. So row 1 and column 1 become zero. First row and column are used as markers.
🧠 Conceptual
expertSpace Complexity of In-Place Set Matrix Zeroes Algorithm
What is the space complexity of the optimized in-place
setZeroes algorithm that uses the first row and first column as markers?Attempts:
2 left
💡 Hint
The algorithm only uses a few variables and modifies the matrix itself.
✗ Incorrect
Using the first row and column as markers avoids extra arrays, so only constant extra space is used.
