Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to iterate over each row in the matrix.
DSA Rust
for [1] in 0..matrix.len() { // process each row }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'col' instead of a row index variable.
Using a variable name that conflicts with Rust keywords.
✗ Incorrect
We use 'i' as the loop variable to iterate over row indices from 0 to matrix length.
2fill in blank
mediumComplete the code to mark the first column zero flag when a zero is found in the matrix.
DSA Rust
if matrix[i][0] == 0 { [1] = true; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable that tracks rows instead of the first column.
Forgetting to set the flag when zero is found.
✗ Incorrect
We set 'first_col_zero' to true to remember that the first column needs to be zeroed later.
3fill in blank
hardFix the error in the code to correctly mark zeroes in the first row and column.
DSA Rust
if matrix[i][j] == 0 { matrix[i][0] = 0; matrix[[1]][j] = 0; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i' instead of 0 for the first row index.
Using 'j' which is the column index, not row.
✗ Incorrect
We mark the first row at column j by setting matrix[0][j] = 0 to track zero columns.
4fill in blank
hardFill both blanks to correctly zero out cells based on markers in first row and column.
DSA Rust
for i in (1..matrix.len()).rev() { for j in (1..matrix[0].len()).rev() { if matrix[i][0] == [1] || matrix[0][j] == [2] { matrix[i][j] = 0; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using boolean values instead of integer zero.
Using 1 which is not the marker for zero.
✗ Incorrect
We check if the first column or first row has zero marker (0) to set matrix[i][j] to zero.
5fill in blank
hardFill all three blanks to zero out the first row and first column if needed.
DSA Rust
if first_col_zero { for i in 0..matrix.len() { matrix[i][[1]] = 0; } } if matrix.len() > 0 && matrix[0].len() > 0 { for j in 0..matrix[0].len() { matrix[[2]][j] = 0; } } let first_row_zero = matrix[[3]].iter().any(|&x| x == 0);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 for first row or column.
Confusing row and column indices.
✗ Incorrect
We zero out the first column (index 0), the first row (index 0), and check if the first row (index 0) has any zero.
