What is the main idea behind searching in a 2D matrix where each row and column is sorted?
Start from the top-right corner and move left if the current number is greater than the target, or move down if it is smaller. This way, you eliminate a row or column each step.
Click to reveal answer
beginner
Why is starting from the top-right corner effective in searching a sorted 2D matrix?
Because from the top-right, moving left decreases the value and moving down increases the value, helping to decide the direction to move based on comparison with the target.
Click to reveal answer
intermediate
What is the time complexity of searching in a sorted 2D matrix using the top-right corner approach?
O(m + n), where m is the number of rows and n is the number of columns, because each step moves either one row down or one column left.
Click to reveal answer
beginner
Show the step-by-step search process for target 5 in this matrix:
[[1, 4, 7],
[2, 5, 8],
[3, 6, 9]]
Start at top-right (7): 7 > 5, move left to 4: 4 < 5, move down to 5: found target.
Click to reveal answer
beginner
What happens if the target is not in the matrix when using the top-right corner search method?
The search moves out of the matrix boundaries (row index exceeds or column index becomes negative), and the target is concluded to be absent.
Click to reveal answer
In a sorted 2D matrix, where should you start searching for a target to use the efficient approach?
ATop-right corner
BBottom-left corner
CTop-left corner
DBottom-right corner
✗ Incorrect
Starting at the top-right corner allows moving left or down to eliminate rows or columns efficiently.
If the current number is greater than the target during the search, what is the next move?
AMove down
BMove right
CMove left
DMove up
✗ Incorrect
Moving left decreases the number, helping to find smaller values closer to the target.
What is the time complexity of searching in a sorted 2D matrix using the top-right corner method?
AO(m * n)
BO(m + n)
CO(1)
DO(log(m * n))
✗ Incorrect
Each step moves either one row down or one column left, so total steps are at most m + n.
If the target is smaller than the current number, which direction do you move?
ALeft
BDown
CRight
DUp
✗ Incorrect
Moving left leads to smaller numbers in the sorted matrix.
What condition ends the search when the target is not found?
AColumn index < 0 only
BColumn index > number of columns
CRow index < 0
DRow index >= number of rows or column index < 0
✗ Incorrect
Search ends when row index goes beyond last row or column index goes before first column.
Explain how to search for a target in a 2D matrix where each row and column is sorted.
Think about how to eliminate rows or columns step by step.
You got /5 concepts.
Describe the time complexity and why the top-right corner approach is efficient for searching in a sorted 2D matrix.
Consider how many steps maximum the search can take.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind searching a target value in a 2D matrix where each row and column is sorted in ascending order?
easy
A. Use binary search on each row independently
B. Start from bottom-left corner and move right or down based on comparison
C. Search each element one by one from top-left to bottom-right
D. Start from top-right corner and move left or down based on comparison
Solution
Step 1: Understand matrix properties
Each row and column is sorted ascending, so top-right element is a good start to decide direction.
Step 2: Use top-right corner to guide search
If target is less than current element, move left; if greater, move down.
Final Answer:
Start from top-right corner and move left or down based on comparison -> Option D
Quick Check:
Top-right start = efficient search [OK]
Hint: Start at top-right to decide move direction quickly [OK]
Common Mistakes:
Searching all elements one by one wastes time
Starting from bottom-left without clear logic
Using binary search on each row ignores column sorting
2. Which of the following is the correct syntax to check if a target exists in a 2D matrix named matrix in JavaScript?
easy
A. for(let i=0; i
B. for(let i=0; i
C. for(let i=0; i
D. for(let i=0; i
Solution
Step 1: Identify correct loop ranges
Outer loop runs over rows (matrix.length), inner loop over columns (matrix[0].length).
Step 2: Check element equality with strict equality
Use === for exact match and return true if found.
Final Answer:
for(let i=0; i -> Option B
Quick Check:
Correct loop ranges and strict equality [OK]
Hint: Outer loop rows, inner loop columns, use === for comparison [OK]
Common Mistakes:
Swapping row and column lengths in loops
Using == instead of === causing type coercion
Looping inner loop with wrong length causing errors
3. What will be the output of the following code snippet?
Initial position is matrix[0][3] = 11, compare with target 5.
Step 2: Move left since 11 > 5, now at matrix[0][2] = 7
7 > 5, move left again to matrix[0][1] = 4.
Step 3: 4 < 5, move down to matrix[1][1] = 5
Found target 5, return true.
Final Answer:
true -> Option C
Quick Check:
Top-right search finds 5 = true [OK]
Hint: Move left if bigger, down if smaller from top-right [OK]
Common Mistakes:
Starting from wrong corner causing wrong moves
Not updating row or column correctly
Returning false too early without full search
4. Find the bug in the following code that searches for a target in a 2D matrix where rows and columns are sorted.
function searchMatrix(matrix, target) {
let row = matrix.length - 1;
let col = 0;
while(row >= 0 && col < matrix[0].length) {
if(matrix[row][col] === target) return true;
else if(matrix[row][col] < target) row--;
else col++;
}
return false;
}
medium
A. The directions to move row and col are swapped in conditions
B. The initial row and col values are incorrect
C. The while loop condition should use '||' instead of '&&'
D. The function should return false inside the loop
Solution
Step 1: Analyze movement logic
Starting at bottom-left, if current < target, should move right (col++), else move up (row--).
Step 2: Check conditions in code
Code moves row-- when current < target, which is wrong; it should move col++ instead.
Final Answer:
The directions to move row and col are swapped in conditions -> Option A
Quick Check:
Correct move directions fix search [OK]
Hint: From bottom-left, move right if smaller, up if bigger [OK]
Common Mistakes:
Swapping row and column moves
Wrong initial position for search
Incorrect loop condition causing infinite loop
5. Given a 2D matrix where each row and column is sorted ascending, write a function to count how many times a target appears in the matrix efficiently. Which approach is best?
hard
A. Start from top-right and move left/down counting matches (O(m+n))
B. Traverse entire matrix counting matches (O(m*n))
C. Use binary search on each row and sum counts (O(m*log n))
D. Flatten matrix to 1D array and use binary search once (O(m*n))
Solution
Step 1: Understand time complexities
Full traversal is slow (O(m*n)), binary search per row is better but still costly.
Step 2: Use top-right start to count efficiently
Moving left/down lets you skip rows or columns, counting matches in O(m+n) time.
Final Answer:
Start from top-right and move left/down counting matches (O(m+n)) -> Option A
Quick Check:
Top-right traversal counts efficiently [OK]
Hint: Top-right start counts matches in linear time [OK]
Common Mistakes:
Using full traversal wasting time
Flattening matrix losing sorted structure
Binary search per row misses column info
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.