Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return false if the matrix is empty.
DSA Typescript
if (matrix.length === [1]) return false;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking matrix[0].length instead of matrix.length
Returning true instead of false when empty
✗ Incorrect
If the matrix has zero rows, it means it's empty, so return false.
2fill in blank
mediumComplete the code to set the number of columns in the matrix.
DSA Typescript
const cols = matrix[0].[1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using matrix.length instead of matrix[0].length
Using a non-existent property like size
✗ Incorrect
The length property gives the number of columns in the first row.
3fill in blank
hardFix the error in the while loop condition to search correctly.
DSA Typescript
while (row < rows && col >= [1]) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using cols instead of 0, which causes out-of-bound errors
Using -1 which is invalid index
✗ Incorrect
Column index should be greater or equal to 0 to stay inside matrix bounds.
4fill in blank
hardFill both blanks to move the search correctly in the matrix.
DSA Typescript
if (matrix[row][col] === target) return true; else if (matrix[row][col] < target) row[1]; else col[2];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- for row increment or ++ for column decrement
Using += or -= which are valid but not the simplest here
✗ Incorrect
If current value is less than target, move down (row++). Else move left (col--).
5fill in blank
hardFill all three blanks to complete the function that searches the matrix.
DSA Typescript
function searchMatrix(matrix: number[][], target: number): boolean {
if (matrix.length === 0) return false;
const rows = matrix.length;
const cols = matrix[0].[1];
let row = 0;
let col = cols [2] 1;
while (row < rows && col >= [3]) {
if (matrix[row][col] === target) return true;
else if (matrix[row][col] < target) row++;
else col--;
}
return false;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using cols instead of cols - 1 for starting column
Using 1 or -1 incorrectly in loop condition
Using + instead of - for col initialization
✗ Incorrect
cols is matrix[0].length; col starts at cols - 1; col must be >= 0 in loop.