Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the matrix is empty before searching.
DSA Rust
if matrix.is_empty() || matrix[0].is_empty() { [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Returning true when matrix is empty
Not checking if matrix[0] is empty
✗ Incorrect
If the matrix or its first row is empty, the target cannot be found, so return false.
2fill in blank
mediumComplete the code to calculate the middle index in binary search.
DSA Rust
let mid = left + (right - left) [1] 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division
Using addition instead of division
✗ Incorrect
To find the middle index, we add left to half the distance between right and left using division by 2.
3fill in blank
hardFix the error in the comparison to move the search window.
DSA Rust
if matrix[row][col] [1] target { left = mid + 1; } else { right = mid - 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<'
Using '==' in condition instead of comparison
✗ Incorrect
If the current element is less than the target, move the left boundary up to search higher values.
4fill in blank
hardFill both blanks to convert mid index to 2D row and column.
DSA Rust
let row = mid [1] n_cols; let col = mid [2] n_cols;
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping division and modulo
Using multiplication instead of division
✗ Incorrect
Row is mid divided by number of columns; column is mid modulo number of columns.
5fill in blank
hardFill all three blanks to complete the binary search loop condition and update pointers.
DSA Rust
while [1] <= [2] { let mid = [3] + (right - left) / 2; // rest of the code }
Attempts:
3 left
💡 Hint
Common Mistakes
Using mid in loop condition
Swapping left and right in condition
✗ Incorrect
The loop runs while left is less than or equal to right; mid is calculated from left and right.
