Challenge - 5 Problems
2D Matrix Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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));
Attempts:
2 left
💡 Hint
Trace the search starting from the top-right corner of the matrix.
✗ Incorrect
The search starts at the top-right element (0,2) which is 5. Since it matches the target, the function returns [0, 2].
❓ Predict Output
intermediate2: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));
Attempts:
2 left
💡 Hint
Check the matrix elements row by row and column by column as per the search logic.
✗ Incorrect
The search finds 8 at position (1, 0) in the matrix and returns [1, 0].
❓ Predict Output
advanced2: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));
Attempts:
2 left
💡 Hint
The search stops at the first found occurrence from top-right.
✗ Incorrect
The search starts at (0,2) which is 3, so it returns [0, 2] immediately.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how values change when moving left or down from the top-right.
✗ Incorrect
Starting at top-right allows moving left to smaller values and down to larger values, efficiently narrowing the search space.
🔧 Debug
expert2: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));
Attempts:
2 left
💡 Hint
Check the initial value of 'col' and how it is used as an index.
✗ Incorrect
Setting col = matrix[0].length sets col to 3, which is out of bounds for indices 0-2. Accessing matrix[row][3] is undefined, causing a TypeError when trying to compare or access properties.