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
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));Attempts:
2 left
💡 Hint
Start from top-right corner and move left or down based on comparison.
✗ Incorrect
The function starts at top-right (11), since 11 > 5, it moves left to 7, then 4, then moves down to 5 which matches the target, so returns true.
❓ Predict Output
intermediate2: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));Attempts:
2 left
💡 Hint
If the target is not found, the function returns false.
✗ Incorrect
The function moves through the matrix but never finds 15, so it returns false.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how the matrix is sorted and how moving left or down changes values.
✗ Incorrect
The matrix rows and columns are sorted ascending. From top-right, moving left decreases values, moving down increases values, allowing efficient elimination of rows or columns.
🔧 Debug
advanced2: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));Attempts:
2 left
💡 Hint
Check the initial value of col and array indexing.
✗ Incorrect
col is initialized to matrix[0].length which is out of bounds (indexing starts at 0), so accessing matrix[row][col] causes RangeError.
🚀 Application
expert2: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?
Attempts:
2 left
💡 Hint
Consider the path the search pointer takes from top-right to bottom-left.
✗ Incorrect
The search moves at most n steps down and n steps left, so maximum 2n elements are checked.