Challenge - 5 Problems
Matrix Zeroes Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after setting zeroes in matrix
What is the state of the matrix after running the Set Matrix Zeroes In Place algorithm on the given input?
DSA Rust
let mut matrix = vec![
vec![1, 1, 1],
vec![1, 0, 1],
vec![1, 1, 1],
];
// After calling set_zeroes(&mut matrix)
// What is the matrix now?Attempts:
2 left
💡 Hint
Remember that if an element is zero, its entire row and column must be set to zero.
✗ Incorrect
The zero at position (1,1) causes the entire row 1 and column 1 to be zeroed. So the matrix becomes [[1,0,1],[0,0,0],[1,0,1]].
❓ Predict Output
intermediateResulting matrix after zeroing rows and columns
Given the matrix below, what is the matrix after applying the Set Matrix Zeroes In Place algorithm?
DSA Rust
let mut matrix = vec![
vec![0, 2, 3],
vec![4, 5, 6],
vec![7, 8, 0],
];
// After set_zeroes(&mut matrix), what is matrix?Attempts:
2 left
💡 Hint
Zeros in first row and last column affect their rows and columns.
✗ Incorrect
Zeros at (0,0) and (2,2) cause row 0, column 0, row 2, and column 2 to be zeroed. So matrix[0][1] and matrix[1][0], matrix[1][2], matrix[2][0..2] are zero.
🔧 Debug
advancedIdentify the error in this Set Matrix Zeroes implementation
What error will this Rust code cause when run on an empty matrix?
DSA Rust
fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
let rows = matrix.len();
let cols = matrix[0].len();
let mut zero_rows = vec![false; rows];
let mut zero_cols = vec![false; cols];
for i in 0..rows {
for j in 0..cols {
if matrix[i][j] == 0 {
zero_rows[i] = true;
zero_cols[j] = true;
}
}
}
for i in 0..rows {
for j in 0..cols {
if zero_rows[i] || zero_cols[j] {
matrix[i][j] = 0;
}
}
}
}
// What error occurs if matrix is empty?Attempts:
2 left
💡 Hint
Check how matrix[0].len() behaves if matrix is empty.
✗ Incorrect
If matrix is empty, matrix[0] access causes panic because there is no first row.
🧠 Conceptual
advancedSpace complexity of in-place Set Matrix Zeroes algorithm
What is the space complexity of the optimal in-place Set Matrix Zeroes algorithm that uses the first row and first column as markers?
Attempts:
2 left
💡 Hint
The algorithm uses the matrix itself to store zero markers.
✗ Incorrect
By using first row and column as markers, no extra arrays are needed, so space is constant.
🚀 Application
expertAfter applying Set Matrix Zeroes, how many non-zero elements remain?
Given the matrix below, after applying the Set Matrix Zeroes In Place algorithm, how many elements remain non-zero?
DSA Rust
let mut matrix = vec![
vec![1, 2, 3, 4],
vec![5, 0, 7, 8],
vec![9, 10, 11, 12],
vec![0, 14, 15, 16],
];
// After set_zeroes(&mut matrix), count non-zero elements.Attempts:
2 left
💡 Hint
Identify rows and columns with zeros and count remaining non-zero elements.
✗ Incorrect
Zeros at (1,1) and (3,0) zero rows 1 and 3, columns 0 and 1. Remaining non-zero elements are at positions (0,2),(0,3),(2,2),(2,3). Total 4 non-zero elements.
