Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Search in 2D Matrix in DSA Typescript - Interactive Practice

Choose your learning style10 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

Practice

(1/5)
1. What is the main idea behind searching in a 2D matrix where each row and column is sorted in ascending order?
easy
A. Start from the bottom-left corner and move right or up based on comparison
B. Start from the top-right corner and move left or down based on comparison
C. Search each element row by row until the target is found
D. Use binary search on each row independently

Solution

  1. Step 1: Understand matrix properties

    Since each row and column is sorted ascending, the top-right element is the largest in its row and smallest in its column.
  2. Step 2: Use top-right corner to decide movement

    If the target is less than the current element, move left; if greater, move down. This efficiently narrows search space.
  3. Final Answer:

    Start from the top-right corner and move left or down based on comparison -> Option B
  4. Quick Check:

    Top-right corner search = Start from the top-right corner and move left or down based on comparison [OK]
Hint: Start at top-right, move left if bigger, down if smaller [OK]
Common Mistakes:
  • Starting from bottom-left without adjusting moves correctly
  • Searching row by row wastes time
  • Using binary search on rows ignores column sorting
2. Which of the following TypeScript code snippets correctly initializes a 2D matrix of numbers?
easy
A. const matrix: number[][] = [[1,2,3],[4,5],[6,7,8,9]];
B. const matrix: number[] = [1,2,3,4,5,6,7,8,9];
C. const matrix: number[][] = [[1,2,3],[4,5,6],[7,8,9]];
D. const matrix: number[][] = [1,2,3,4,5,6,7,8,9];

Solution

  1. Step 1: Check matrix type declaration

    TypeScript 2D array is declared as number[][]. const matrix: number[][] = [[1,2,3],[4,5,6],[7,8,9]];.
  2. Step 2: Validate matrix structure

    const matrix: number[][] = [[1,2,3],[4,5,6],[7,8,9]]; has uniform rows with 3 elements each, valid for a matrix. const matrix: number[][] = [[1,2,3],[4,5],[6,7,8,9]]; has rows of different lengths, which is allowed but less common for matrices.
  3. Final Answer:

    const matrix: number[][] = [[1,2,3],[4,5,6],[7,8,9]]; -> Option C
  4. Quick Check:

    Proper 2D array syntax = const matrix: number[][] = [[1,2,3],[4,5,6],[7,8,9]]; [OK]
Hint: Use number[][] with nested arrays for 2D matrix [OK]
Common Mistakes:
  • Using single array for 2D matrix
  • Incorrect type declaration like number[] for 2D
  • Mixing array elements without nested arrays
3. What will be the output of the following TypeScript code?
const matrix = [
  [1, 4, 7, 11],
  [2, 5, 8, 12],
  [3, 6, 9, 16],
  [10, 13, 14, 17]
];

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;
}

console.log(searchMatrix(matrix, 5));
medium
A. Runtime error
B. false
C. undefined
D. true

Solution

  1. Step 1: Start search from top-right corner

    Initial position is row=0, col=3 (value 11). Since 11 > 5, move left to col=2 (value 7).
  2. Step 2: Compare and move accordingly

    7 > 5, move left to col=1 (value 4). 4 < 5, move down to row=1, col=1 (value 5). Found target 5, return true.
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    Search finds 5 = true [OK]
Hint: Start top-right, move left if bigger, down if smaller [OK]
Common Mistakes:
  • Not updating row and col correctly
  • Returning false too early
  • Confusing row and column indices
4. Identify the bug in this TypeScript function that searches a target in a sorted 2D matrix:
function searchMatrix(matrix: number[][], target: number): boolean {
  let row = 0;
  let col = matrix.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;
}
medium
A. col should be initialized to matrix[0].length - 1, not matrix.length - 1
B. row should start from matrix.length - 1
C. The while loop condition should use col < matrix[0].length
D. The function should return true at the end instead of false

Solution

  1. Step 1: Check initialization of col variable

    col is set to matrix.length - 1, which is number of rows minus one, but columns count is matrix[0].length.
  2. Step 2: Correct col initialization

    col should be matrix[0].length - 1 to point to the last column index in the first row.
  3. Final Answer:

    col should be initialized to matrix[0].length - 1, not matrix.length - 1 -> Option A
  4. Quick Check:

    col init must match columns count = col should be initialized to matrix[0].length - 1, not matrix.length - 1 [OK]
Hint: col index = last column, use matrix[0].length - 1 [OK]
Common Mistakes:
  • Using rows count for column index
  • Wrong loop conditions causing out-of-bounds
  • Returning true instead of false when not found
5. Given a 2D matrix where each row and column is sorted ascending, how would you modify the search function to return the position [row, col] of the target if found, or [-1, -1] if not found? Choose the correct code snippet.
function searchMatrix(matrix: number[][], target: number): number[] {
  let row = 0;
  let col = matrix[0].length - 1;
  while (row < matrix.length && col >= 0) {
    if (matrix[row][col] === target) {
      // What to return here?
    } else if (matrix[row][col] > target) {
      col--;
    } else {
      row++;
    }
  }
  // What to return if not found?
}
hard
A. Return [row, col] inside if, and [-1, -1] after loop
B. Return [col, row] inside if, and [0, 0] after loop
C. Return true inside if, and false after loop
D. Return matrix[row][col] inside if, and null after loop

Solution

  1. Step 1: Return position when target found

    Inside the if condition, return the current indices as [row, col] to indicate target position.
  2. Step 2: Return [-1, -1] if target not found

    After the loop ends without finding target, return [-1, -1] to indicate absence.
  3. Final Answer:

    Return [row, col] inside if, and [-1, -1] after loop -> Option A
  4. Quick Check:

    Return position or [-1, -1] if missing = Return [row, col] inside if, and [-1, -1] after loop [OK]
Hint: Return indices when found, [-1, -1] if not found [OK]
Common Mistakes:
  • Swapping row and col in return
  • Returning boolean instead of position
  • Returning wrong default value when not found
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard