What is the main idea behind searching in a 2D matrix where each row and column is sorted?
Start from the top-right or bottom-left corner and move step-by-step eliminating rows or columns based on comparisons to find the target efficiently.
Click to reveal answer
beginner
In a sorted 2D matrix, why is starting from the top-right corner helpful for search?
Because from the top-right, moving left decreases values and moving down increases values, allowing you to decide which direction to go based on the target.
Click to reveal answer
intermediate
What is the time complexity of searching in a sorted 2D matrix using the stepwise linear search method?
O(m + n), where m is the number of rows and n is the number of columns, because you move at most m steps down and n steps left.
Click to reveal answer
intermediate
Explain the binary search approach for searching in a 2D matrix where the entire matrix is sorted as if it was a flat array.
Treat the matrix as a single sorted list by mapping 1D indices to 2D positions, then apply binary search to find the target in O(log(m*n)) time.
Click to reveal answer
intermediate
What is a key difference between the stepwise linear search and binary search methods in a 2D matrix?
Stepwise linear search requires each row and column to be sorted separately, while binary search requires the entire matrix to be sorted as a single list.
Click to reveal answer
Where do you start searching in a 2D matrix sorted by rows and columns for efficient search?
ABottom-right corner
BTop-left corner
CTop-right corner
DCenter of the matrix
✗ Incorrect
Starting at the top-right corner allows moving left or down to eliminate rows or columns based on comparisons.
What is the worst-case time complexity of stepwise linear search in a sorted 2D matrix with m rows and n columns?
AO(m * n)
BO(m + n)
CO(log(m * n))
DO(1)
✗ Incorrect
You move at most m steps down and n steps left, so total steps are O(m + n).
Which condition must be true to apply binary search treating the 2D matrix as a flat sorted array?
AThe entire matrix is sorted in row-major order
BEach column is sorted, but rows are not
CNo sorting is required
DEach row is sorted, but columns are not
✗ Incorrect
Binary search requires the entire matrix to be sorted as if it was a single sorted list.
If the target is less than the current element at top-right, what is the next step in stepwise linear search?
AMove left one column
BMove down one row
CMove right one column
DMove up one row
✗ Incorrect
Since the target is smaller, move left to find smaller elements.
What is the main advantage of stepwise linear search over binary search in a 2D matrix?
AWorks on unsorted matrices
BFaster time complexity
CRequires less memory
DWorks even if only rows and columns are sorted separately
✗ Incorrect
Stepwise linear search only needs each row and column sorted, not the entire matrix.
Describe how to search for a target value in a 2D matrix where each row and column is sorted.
Think about eliminating rows or columns step-by-step.
You got /5 concepts.
Explain the difference between stepwise linear search and binary search in a 2D matrix.
Consider sorting requirements and time complexity.
You got /5 concepts.
Practice
(1/5)
1. What is the main idea behind searching a target value in a 2D matrix where each row is sorted and the first integer of each row is greater than the last integer of the previous row?
easy
A. Use binary search treating the matrix as a flattened sorted list
B. Search each row linearly from left to right
C. Search each column linearly from top to bottom
D. Use a hash map to store all elements for quick lookup
Solution
Step 1: Understand matrix sorting property
Each row is sorted and the first element of a row is greater than the last element of the previous row, so the entire matrix can be seen as a sorted list.
Step 2: Apply binary search on the flattened matrix
We can treat the matrix as a single sorted list and use binary search to find the target efficiently.
Final Answer:
Use binary search treating the matrix as a flattened sorted list -> Option A
Quick Check:
Matrix sorted row-wise + binary search = Use binary search treating the matrix as a flattened sorted list [OK]
Hint: Think of the matrix as one sorted list for binary search [OK]
Common Mistakes:
Searching each row or column linearly wastes time
Using hash map wastes extra space
Ignoring the matrix's sorted property
2. Which Rust code snippet correctly calculates the middle index for binary search on a 2D matrix treated as a 1D array with total length `len`?
easy
A. let mid = (start + end) / 2;
B. let mid = start + (end - start) / 2;
C. let mid = (start - end) / 2;
D. let mid = (start * end) / 2;
Solution
Step 1: Avoid overflow in mid calculation
Using `(start + end) / 2` can overflow if start and end are large. The safer way is `start + (end - start) / 2`.
Step 2: Check other options
let mid = (start - end) / 2; subtracts incorrectly, and D multiplies which is wrong for mid calculation.
Hint: Use start + (end - start) / 2 to avoid overflow [OK]
Common Mistakes:
Using (start + end) / 2 causing overflow
Using subtraction incorrectly for mid
Multiplying indices instead of averaging
3. Given the following Rust code to search for target 5 in a 2D matrix, what will be the printed output?
let matrix = vec![
vec![1, 3, 5, 7],
vec![10, 11, 16, 20],
vec![23, 30, 34, 60]
];
let target = 5;
let rows = matrix.len();
let cols = matrix[0].len();
let mut start = 0;
let mut end = rows * cols - 1;
let mut found = false;
while start <= end {
let mid = start + (end - start) / 2;
let r = mid / cols;
let c = mid % cols;
if matrix[r][c] == target {
found = true;
break;
} else if matrix[r][c] < target {
start = mid + 1;
} else {
end = mid - 1;
}
}
println!("{}", found);
medium
A. Runtime panic
B. false
C. Compilation error
D. true
Solution
Step 1: Understand binary search on matrix
The code treats the matrix as a sorted list and uses binary search to find target 5.
Step 2: Trace the search for target 5
5 is at matrix[0][2]. The binary search will find it and set found = true.
Final Answer:
true -> Option D
Quick Check:
Target 5 found = true [OK]
Hint: Map mid to row and col, then compare target [OK]
Common Mistakes:
Confusing row and column calculation
Off-by-one errors in indices
Not updating start or end correctly
4. Identify the bug in this Rust code snippet that searches for a target in a 2D matrix and suggest the fix:
let matrix = vec![
vec![1, 4, 7, 11],
vec![15, 20, 25, 30],
vec![35, 40, 45, 50]
];
let target = 20;
let rows = matrix.len();
let cols = matrix[0].len();
let mut start = 0;
let mut end = rows * cols;
while start <= end {
let mid = start + (end - start) / 2;
let r = mid / cols;
let c = mid % cols;
if matrix[r][c] == target {
println!("Found");
break;
} else if matrix[r][c] < target {
start = mid + 1;
} else {
end = mid - 1;
}
}
medium
A. end should be rows * cols - 1 instead of rows * cols
B. start should be 1 instead of 0
C. Use while start < end instead of while start <= end
D. Calculate r and c as mid % cols and mid / cols respectively
Solution
Step 1: Check the range of end index
Matrix has rows * cols elements, so last index is rows * cols - 1, not rows * cols.
Step 2: Fix end initialization
Set end = rows * cols - 1 to avoid out-of-bound access when mid equals rows * cols.
Final Answer:
end should be rows * cols - 1 instead of rows * cols -> Option A
Quick Check:
Index range off-by-one fixed = end should be rows * cols - 1 instead of rows * cols [OK]
Hint: End index = total elements - 1 to avoid overflow [OK]
Common Mistakes:
Setting end to rows * cols causing panic
Swapping row and column calculations
Incorrect loop condition
5. You have a 2D matrix where each row is sorted in ascending order, but the first integer of each row may be smaller than the last integer of the previous row. Which approach correctly searches for a target value efficiently?
hard
A. Use binary search on each row separately until target is found or all rows checked
B. Treat the matrix as a flattened sorted list and apply binary search once
C. Start from top-right corner and move left or down based on comparison
D. Use a hash set to store all elements and check membership
Solution
Step 1: Understand matrix property
Rows are sorted but no guarantee that first element of a row is greater than last of previous row, so flattened binary search won't work.
Step 2: Use stair-step search from top-right
Starting at top-right, move left if current > target, move down if current < target, efficiently narrowing search.
Final Answer:
Start from top-right corner and move left or down based on comparison -> Option C
Quick Check:
Stair-step search fits matrix property = Start from top-right corner and move left or down based on comparison [OK]
Hint: Start top-right, move left/down to find target fast [OK]
Common Mistakes:
Assuming full matrix sorted for binary search
Searching each row fully wasting time
Using extra space unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.