Challenge - 5 Problems
Matrix Zeroing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of the matrix after running this code?
Consider the following C code that sets entire rows and columns to zero if an element is zero. What will be the printed matrix after execution?
DSA C
void setZeroes(int matrix[3][3], int rows, int cols) { int rowFlag = 0, colFlag = 0; for (int i = 0; i < rows; i++) { if (matrix[i][0] == 0) colFlag = 1; } for (int j = 0; j < cols; j++) { if (matrix[0][j] == 0) rowFlag = 1; } for (int i = 1; i < rows; i++) { for (int j = 1; j < cols; j++) { if (matrix[i][j] == 0) { matrix[i][0] = 0; matrix[0][j] = 0; } } } for (int i = 1; i < rows; i++) { for (int j = 1; j < cols; j++) { if (matrix[i][0] == 0 || matrix[0][j] == 0) { matrix[i][j] = 0; } } } if (colFlag) { for (int i = 0; i < rows; i++) { matrix[i][0] = 0; } } if (rowFlag) { for (int j = 0; j < cols; j++) { matrix[0][j] = 0; } } } int main() { int matrix[3][3] = { {1, 2, 3}, {4, 0, 6}, {7, 8, 9} }; setZeroes(matrix, 3, 3); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } return 0; }
Attempts:
2 left
💡 Hint
Check how the first row and first column are used as markers and how the flags affect them.
✗ Incorrect
The code uses the first row and first column to mark which rows and columns should be zeroed. The zero at matrix[1][1] sets matrix[1][0] and matrix[0][1] to zero. Then, all elements in row 1 and column 1 except the first row and column are set to zero. The first row and column are updated last based on flags. Here, neither flag is set because there were no original zeros in the first row or first column.
❓ Predict Output
intermediate2:00remaining
What is the output after setting zeroes in this 4x4 matrix?
Given the following matrix and the same setZeroes function, what is the matrix after the function call?
DSA C
int matrix[4][4] = { {1, 2, 3, 4}, {5, 0, 7, 8}, {9, 10, 11, 12}, {0, 14, 15, 16} }; setZeroes(matrix, 4, 4);
Attempts:
2 left
💡 Hint
Remember the first row and column are markers and flags control zeroing of them.
✗ Incorrect
Zeros at matrix[1][1] and matrix[3][0] mark row 1, row 3, column 1, and column 0 for zeroing. The first row and column flags are set accordingly. After processing, rows 1 and 3 and columns 0 and 1 are zeroed except the first row and column which are zeroed last based on flags.
🔧 Debug
advanced2:00remaining
Why does this code cause incorrect zeroing in some cases?
This code attempts to set matrix zeroes in place but sometimes fails. What is the main logical error?
DSA C
void setZeroes(int** matrix, int rows, int cols) { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (matrix[i][j] == 0) { for (int k = 0; k < cols; k++) matrix[i][k] = 0; for (int k = 0; k < rows; k++) matrix[k][j] = 0; } } } }
Attempts:
2 left
💡 Hint
Think about what happens when you change the matrix while looping through it.
✗ Incorrect
The code zeroes rows and columns immediately when it finds a zero. This changes the matrix during iteration, causing zeros introduced by zeroing to trigger more zeroing incorrectly. The correct approach is to mark first, then zero after scanning.
❓ Predict Output
advanced2:00remaining
What is the output of this 2x2 matrix after setZeroes?
Given this 2x2 matrix and the setZeroes function, what is the matrix after execution?
DSA C
int matrix[2][2] = { {0, 1}, {1, 1} }; setZeroes(matrix, 2, 2);
Attempts:
2 left
💡 Hint
Check how the first row and first column flags affect zeroing.
✗ Incorrect
The zero at matrix[0][0] sets the first row and first column flags. The first column flag is set, so the first column is zeroed. The first row flag is also set, so the first row is zeroed. But since only matrix[0][0] is zero, only the first row and column are zeroed accordingly. The element matrix[1][1] remains 1.
🧠 Conceptual
expert2:00remaining
Why is using the first row and column as markers optimal for in-place zeroing?
In the in-place set matrix zeroes algorithm, why do we use the first row and first column to store markers instead of extra memory?
Attempts:
2 left
💡 Hint
Think about memory usage and how markers are stored.
✗ Incorrect
Using the first row and column as markers avoids allocating extra arrays for rows and columns to be zeroed. This keeps space complexity constant (O(1)) while maintaining time complexity at O(n*m).
