0
0
DSA Javascriptprogramming~20 mins

Search in 2D Matrix in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
2D Matrix Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of search in sorted 2D matrix
What is the output of the following JavaScript code that searches for the number 5 in a sorted 2D matrix?
DSA Javascript
const matrix = [
  [1, 3, 5],
  [7, 9, 11],
  [13, 15, 17]
];

function searchMatrix(matrix, target) {
  let row = 0;
  let col = matrix[0].length - 1;
  while (row < matrix.length && col >= 0) {
    if (matrix[row][col] === target) {
      return [row, col];
    } else if (matrix[row][col] > target) {
      col--;
    } else {
      row++;
    }
  }
  return [-1, -1];
}

console.log(searchMatrix(matrix, 5));
A[0, 2]
B[2, 0]
C[-1, -1]
D[1, 0]
Attempts:
2 left
💡 Hint
Trace the search starting from the top-right corner of the matrix.
Predict Output
intermediate
2:00remaining
Result of searching for a missing element
What does the following code print when searching for 8 in the given matrix?
DSA Javascript
const matrix = [
  [2, 4, 6],
  [8, 10, 12],
  [14, 16, 18]
];

function searchMatrix(matrix, target) {
  let row = 0;
  let col = matrix[0].length - 1;
  while (row < matrix.length && col >= 0) {
    if (matrix[row][col] === target) {
      return [row, col];
    } else if (matrix[row][col] > target) {
      col--;
    } else {
      row++;
    }
  }
  return [-1, -1];
}

console.log(searchMatrix(matrix, 8));
A[-1, -1]
B[0, 2]
C[1, 0]
D[2, 1]
Attempts:
2 left
💡 Hint
Check the matrix elements row by row and column by column as per the search logic.
Predict Output
advanced
2:00remaining
Output of search in matrix with duplicates
What is the output of this code searching for 3 in a matrix containing duplicates?
DSA Javascript
const matrix = [
  [1, 2, 3],
  [3, 4, 5],
  [5, 6, 7]
];

function searchMatrix(matrix, target) {
  let row = 0;
  let col = matrix[0].length - 1;
  while (row < matrix.length && col >= 0) {
    if (matrix[row][col] === target) {
      return [row, col];
    } else if (matrix[row][col] > target) {
      col--;
    } else {
      row++;
    }
  }
  return [-1, -1];
}

console.log(searchMatrix(matrix, 3));
A[0, 2]
B[1, 0]
C[-1, -1]
D[2, 0]
Attempts:
2 left
💡 Hint
The search stops at the first found occurrence from top-right.
🧠 Conceptual
advanced
2:00remaining
Why start search from top-right corner?
Why is the top-right corner chosen as the starting point for searching a target in a sorted 2D matrix where each row and column is sorted ascending?
ABecause starting from top-right guarantees the target is found in the first step.
BBecause top-right is always the smallest element in the matrix.
CBecause top-right corner is the middle element of the matrix.
DBecause from top-right, moving left decreases values and moving down increases values, allowing efficient elimination.
Attempts:
2 left
💡 Hint
Think about how values change when moving left or down from the top-right.
🔧 Debug
expert
2:00remaining
Identify the error in this 2D matrix search code
What error will this code produce when searching for 10 in the matrix? Identify the exact error type.
DSA Javascript
const matrix = [
  [1, 4, 7],
  [8, 10, 12],
  [14, 16, 18]
];

function searchMatrix(matrix, target) {
  let row = 0;
  let col = matrix[0].length; // Note the difference here
  while (row < matrix.length && col >= 0) {
    if (matrix[row][col] === target) {
      return [row, col];
    } else if (matrix[row][col] > target) {
      col--;
    } else {
      row++;
    }
  }
  return [-1, -1];
}

console.log(searchMatrix(matrix, 10));
ARangeError: Maximum call stack size exceeded
BTypeError: Cannot read property 'undefined' of undefined
CReferenceError: col is not defined
DTypeError: Cannot read property 'length' of undefined
Attempts:
2 left
💡 Hint
Check the initial value of 'col' and how it is used as an index.