0
0
DSA Typescriptprogramming~10 mins

Search in 2D Matrix in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A1
B0
Cnull
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Checking matrix[0].length instead of matrix.length
Returning true instead of false when empty
2fill in blank
medium

Complete 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'
Alength
Bsize
Ccount
Dwidth
Attempts:
3 left
💡 Hint
Common Mistakes
Using matrix.length instead of matrix[0].length
Using a non-existent property like size
3fill in blank
hard

Fix 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'
A-1
Bcols
C0
Drows
Attempts:
3 left
💡 Hint
Common Mistakes
Using cols instead of 0, which causes out-of-bound errors
Using -1 which is invalid index
4fill in blank
hard

Fill 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'
A++
B--
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Using -- for row increment or ++ for column decrement
Using += or -= which are valid but not the simplest here
5fill in blank
hard

Fill 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'
Alength
B-
C0
D+
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