Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the matrix is empty.
DSA C#
if (matrix == null || matrix.Length == 0 || matrix[0].Length [1] 0) { return false; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<' instead of '==' causes wrong condition checks.
Using '!=' will not correctly detect empty matrix.
✗ Incorrect
We check if the first row length is equal to zero to confirm the matrix has no columns.
2fill in blank
mediumComplete the code to initialize the number of rows in the matrix.
DSA C#
int rows = matrix.[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using Count causes compile error because arrays do not have Count property.
Using GetLength without parameters is incorrect.
✗ Incorrect
In C#, arrays use Length property to get the number of elements in the first dimension.
3fill in blank
hardFix the error in the binary search loop condition to avoid infinite loop.
DSA C#
while (left [1] right) { int mid = left + (right - left) / 2; int midValue = matrix[mid / cols][mid % cols]; if (midValue == target) return true; else if (midValue < target) left = mid + 1; else right = mid - 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes missing the last element check.
Using '>' or '>=' reverses the logic and causes infinite loop.
✗ Incorrect
The binary search loop should run while left is less than or equal to right to cover all elements.
4fill in blank
hardFill both blanks to correctly calculate the middle element's row and column indices.
DSA C#
int midRow = mid [1] cols; int midCol = mid [2] cols;
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or subtraction instead of division/modulo.
Swapping division and modulo operators.
✗ Incorrect
Row index is mid divided by number of columns; column index is mid modulo number of columns.
5fill in blank
hardFill all three blanks to complete the binary search in the 2D matrix.
DSA C#
int left = 0, right = rows [1] cols [2] 1; while (left [3] right) { int mid = left + (right - left) / 2; int midValue = matrix[mid / cols][mid % cols]; if (midValue == target) return true; else if (midValue < target) left = mid + 1; else right = mid - 1; } return false;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' for right boundary causes index out of range.
Using '<' instead of '<=' misses last element check.
✗ Incorrect
The right boundary is rows * cols - 1; the loop runs while left <= right.
