Practice
1. Given the following code snippet, what is the final return value when calling
minDays([1,10,3,10,2], 3, 1)?easy
Solution
Step 1: Trace binary search boundaries
min bloom day = 1, max bloom day = 10. Binary search mid values: 5, then 3, then 2.Step 2: Check feasibility at mid=5 and mid=2
At day=5, canMake returns true (flowers bloomed on days 1,3,2). Try lower days. At day=2, canMake returns true as well. At day=1, canMake returns false. So minimum day is 2.Final Answer:
Option A -> Option AQuick Check:
Tracing confirms minimum feasible day is 2. [OK]
Hint: Check feasibility at midpoints to narrow minimum day [OK]
Common Mistakes:
- Off-by-one errors in binary search boundaries
- Miscounting bouquets in feasibility check
2. You need to find the minimum speed to travel through a series of distances within a given total time, where each segment except the last must be completed in whole hours (rounded up). Which algorithmic approach guarantees finding the minimum feasible speed efficiently?
easy
Solution
Step 1: Understand the problem constraints
The problem requires finding the minimum speed that satisfies a time constraint with rounding up travel times for all but the last segment.Step 2: Identify the suitable algorithmic pattern
Binary search on the answer space (speed) efficiently narrows down the minimum feasible speed by checking feasibility at midpoints, unlike greedy or DP which do not fit the problem structure.Final Answer:
Option A -> Option AQuick Check:
Binary search on answer space is standard for optimization problems with monotonic feasibility [OK]
Hint: Binary search on answer space fits monotonic feasibility [OK]
Common Mistakes:
- Thinking greedy or DP can solve this efficiently
- Using brute force linear search without pruning
3. Consider the following code snippet that searches for a target in a 2D matrix using binary search treating it as a 1D array. Given the matrix = [[1,4],[6,8]] and target = 6, what is the value of
mid and the corresponding row, col during the first iteration of the while loop?easy
Solution
Step 1: Calculate initial boundaries and mid
m=2, n=2, left=0, right=3, so mid=(0+3)//2=1.Step 2: Map mid to 2D indices
row, col = divmod(1, 2) -> row=0, col=1.Final Answer:
Option A -> Option AQuick Check:
mid=1 corresponds to matrix[0][1]=4, correct mapping [OK]
Hint: mid index maps to row,col via divmod(mid, n) [OK]
Common Mistakes:
- Off-by-one in mid calculation
- Confusing row and col indices
- Using mid as direct index without divmod
4. Examine the following buggy code that attempts to search a target in a 2D matrix using the staircase search approach starting from the top-left corner. Identify the line containing the subtle bug that causes incorrect behavior or infinite loops.
medium
Solution
Step 1: Analyze starting point
Starting at top-left corner breaks the staircase search logic because we cannot eliminate rows or columns correctly.Step 2: Check pointer movements
Moving row down when current < target and col left when current > target only works if starting at top-right corner.Final Answer:
Option D -> Option DQuick Check:
Starting at top-left causes infinite loops or wrong results [OK]
Hint: Staircase search must start at top-right corner [OK]
Common Mistakes:
- Starting at top-left or bottom-left corner
- Not checking empty matrix
- Moving pointers in wrong directions
5. Suppose the array is no longer sorted but you still need to find the first and last positions of a target value. Which approach is correct and efficient?
hard
Solution
Step 1: Recognize that binary search requires sorted array
Without sorting, binary search cannot guarantee correct positions.Step 2: Evaluate alternatives
Sorting changes indices, so linear scan or hash map are options; linear scan is simplest and O(n).Step 3: Compare efficiency and correctness
Linear scan finds first and last positions in one pass without modifying array.Final Answer:
Option A -> Option AQuick Check:
Binary search requires sorted input; linear scan works otherwise [OK]
Hint: Binary search needs sorted array; linear scan works otherwise [OK]
Common Mistakes:
- Trying binary search on unsorted array
- Sorting and losing original indices
- Overcomplicating with hash maps
