0
0
DSA Typescriptprogramming~20 mins

Search in 2D Matrix in DSA Typescript - 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
What is the output of the search function?
Given the matrix and the target, what will the search function return?
DSA Typescript
function searchMatrix(matrix: number[][], target: number): boolean {
  let row = 0;
  let col = matrix[0].length - 1;
  while (row < matrix.length && col >= 0) {
    if (matrix[row][col] === target) {
      return true;
    } else if (matrix[row][col] > target) {
      col--;
    } else {
      row++;
    }
  }
  return false;
}

const matrix = [
  [1, 4, 7, 11],
  [2, 5, 8, 12],
  [3, 6, 9, 16],
  [10, 13, 14, 17]
];
const target = 5;
console.log(searchMatrix(matrix, target));
Atrue
Bfalse
Cundefined
DTypeError
Attempts:
2 left
💡 Hint
Start from top-right corner and move left or down based on comparison.
Predict Output
intermediate
2:00remaining
What is the output of the search for a missing target?
What will the function return when searching for a target not in the matrix?
DSA Typescript
function searchMatrix(matrix: number[][], target: number): boolean {
  let row = 0;
  let col = matrix[0].length - 1;
  while (row < matrix.length && col >= 0) {
    if (matrix[row][col] === target) {
      return true;
    } else if (matrix[row][col] > target) {
      col--;
    } else {
      row++;
    }
  }
  return false;
}

const matrix = [
  [1, 4, 7, 11],
  [2, 5, 8, 12],
  [3, 6, 9, 16],
  [10, 13, 14, 17]
];
const target = 15;
console.log(searchMatrix(matrix, target));
Aundefined
BRangeError
Cfalse
Dtrue
Attempts:
2 left
💡 Hint
If the target is not found, the function returns false.
🧠 Conceptual
advanced
2:00remaining
Why does the search start from the top-right corner?
Why is the top-right corner chosen as the starting point for searching in a sorted 2D matrix?
ABecause top-right corner is always the smallest element in the matrix.
BBecause it is easier to implement recursion from top-right corner.
CBecause starting from top-right allows binary search on rows only.
DBecause from top-right, we can move left to smaller numbers and down to larger numbers, narrowing search efficiently.
Attempts:
2 left
💡 Hint
Think about how the matrix is sorted and how moving left or down changes values.
🔧 Debug
advanced
2:00remaining
What error does this code produce?
What error will this code produce when run?
DSA Typescript
function searchMatrix(matrix: number[][], target: number): boolean {
  let row = 0;
  let col = matrix[0].length;
  while (row < matrix.length && col >= 0) {
    if (matrix[row][col] === target) {
      return true;
    } else if (matrix[row][col] > target) {
      col--;
    } else {
      row++;
    }
  }
  return false;
}

const matrix = [
  [1, 4, 7, 11],
  [2, 5, 8, 12],
  [3, 6, 9, 16],
  [10, 13, 14, 17]
];
const target = 5;
console.log(searchMatrix(matrix, target));
AIndexError
BRangeError
CTypeError
Dundefined behavior
Attempts:
2 left
💡 Hint
Check the initial value of col and array indexing.
🚀 Application
expert
2:00remaining
How many elements are checked in worst case?
Given an n x n matrix sorted as described, what is the maximum number of elements the search function checks in the worst case?
A2n
Blog n
Cn^2
Dn
Attempts:
2 left
💡 Hint
Consider the path the search pointer takes from top-right to bottom-left.