0
0
DSA Javascriptprogramming~10 mins

Search in 2D Matrix in DSA Javascript - Interactive Practice

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

Complete the code to start searching from the top-right corner of the matrix.

DSA Javascript
let row = 0;
let col = matrix[0].length [1] 1;
Drag options to blanks, or click blank then click option'
A+
B/
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - causes index out of range.
Using * or / is not correct for index calculation.
2fill in blank
medium

Complete the code to move left when the current element is greater than the target.

DSA Javascript
if (matrix[row][col] > target) {
  col [1] 1;
}
Drag options to blanks, or click blank then click option'
A++
B+=
C-=
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using += increases the column index, moving right instead of left.
Using ++ or -- alone changes by 1 but does not assign.
3fill in blank
hard

Fix the error in the loop condition to prevent out-of-bound errors.

DSA Javascript
while (row < matrix.length && col [1] 0) {
  // search logic
}
Drag options to blanks, or click blank then click option'
A<
B>=
C<=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > 0 skips checking column 0.
Using < or <= causes logic errors in loop.
4fill in blank
hard

Fill both blanks to correctly move down or left based on comparison.

DSA Javascript
if (matrix[row][col] < target) {
  row [1] 1;
} else {
  col [2] 1;
}
Drag options to blanks, or click blank then click option'
A+=
B-=
C++
D--
Attempts:
3 left
💡 Hint
Common Mistakes
Using -= to move down or += to move left is incorrect.
Using ++ or -- without assignment causes errors.
5fill in blank
hard

Fill all three blanks to complete the search function returning true if target found, else false.

DSA Javascript
function searchMatrix(matrix, target) {
  let row = 0;
  let col = matrix[0].length [1] 1;
  while (row < matrix.length && col [2] 0) {
    if (matrix[row][col] === target) {
      return [3];
    } else if (matrix[row][col] < target) {
      row += 1;
    } else {
      col -= 1;
    }
  }
  return false;
}
Drag options to blanks, or click blank then click option'
A+
B>=
Ctrue
D-
E<
Ffalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of - for last column index.
Using < or <= in loop condition causing errors.
Returning false instead of true when found.