Challenge - 5 Problems
Matrix Zeroes 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 the code?
Given the matrix below, what will be its state after applying the Set Matrix Zeroes In Place algorithm?
DSA Python
matrix = [ [1, 2, 3], [4, 0, 6], [7, 8, 9] ] # After running the algorithm, print the matrix row-wise
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 zeroed. So, row 1 becomes all zeros, and column 1 elements in other rows become zero.
❓ Predict Output
intermediate2:00remaining
What is the output matrix after applying the algorithm?
Consider this matrix. What will it look like after applying the Set Matrix Zeroes In Place algorithm?
DSA Python
matrix = [ [0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5] ] # Print the matrix row-wise after the algorithm
Attempts:
2 left
💡 Hint
Zeros in the first row cause their columns and rows to be zeroed.
✗ Incorrect
The zeros at positions (0,0) and (0,3) cause row 0 and columns 0 and 3 to be zeroed. Rows 1 and 2 have zeros in columns 0 and 3 respectively.
🔧 Debug
advanced2:00remaining
What error does this code raise when run?
This code tries to set matrix zeroes in place. What error will it raise?
DSA Python
def set_zeroes(matrix): rows = len(matrix) cols = len(matrix[0]) zero_rows = set() zero_cols = set() for i in range(rows): for j in range(cols): if matrix[i][j] == 0: zero_rows.add(i) zero_cols.add(j) for i in zero_rows: for j in range(cols): matrix[i][j] = 0 for j in zero_cols: for i in range(rows): matrix[i][j] = 0 matrix = [[1,2,3],[4,0,6],[7,8,9]] set_zeroes(matrix) print(matrix[3])
Attempts:
2 left
💡 Hint
Check the print statement accessing matrix[3].
✗ Incorrect
The matrix has 3 rows indexed 0,1,2. Accessing matrix[3] is out of range, causing IndexError.
🧠 Conceptual
advanced2:00remaining
How many elements are set to zero after running the algorithm on this matrix?
Given the matrix below, how many elements will be zero after applying the Set Matrix Zeroes In Place algorithm?
DSA Python
matrix = [ [1, 2, 0], [4, 5, 6], [0, 8, 9] ]
Attempts:
2 left
💡 Hint
Count zeros in rows and columns affected by zeros.
✗ Incorrect
Zeros at (0,2) and (2,0) cause row 0, row 2, column 0, and column 2 to be zeroed. Counting all zeroed elements gives 8.
🚀 Application
expert3:00remaining
What is the final matrix after applying the algorithm in place?
Apply the Set Matrix Zeroes In Place algorithm on this matrix and provide the final matrix state.
DSA Python
matrix = [ [1, 2, 3, 4], [5, 0, 7, 8], [9, 10, 11, 12], [0, 14, 15, 16] ] # Print matrix row-wise after algorithm
Attempts:
2 left
💡 Hint
Zeros at (1,1) and (3,0) affect their rows and columns.
✗ Incorrect
The zero at (1,1) zeroes row 1 and column 1. The zero at (3,0) zeroes row 3 and column 0. Combining these, the final matrix is as in option A.