Practice
1. You are given a sorted array and need to find how many times a specific target value appears. Which algorithmic approach guarantees the most efficient solution in terms of time complexity?
easy
Solution
Step 1: Understand problem constraints
The array is sorted, so binary search can be used to find boundaries efficiently.Step 2: Identify optimal approach
Binary search for left and right boundaries finds the first and last occurrence indices in O(log n) time, enabling count calculation by subtraction.Final Answer:
Option A -> Option AQuick Check:
Binary search halves search space each step [OK]
Hint: Sorted array -> binary search boundaries [OK]
Common Mistakes:
- Using linear scan despite sorted array
- Confusing DP with counting occurrences
- Applying greedy which doesn't guarantee correctness
2. Given the following code and input
nums = [2, 4, 4, 6] and target = 4, what is the final returned value?easy
Solution
Step 1: Trace binary search to find any occurrence of 4
Initial left=0, right=3, mid=1, nums[1]=4 matches target, found=1.Step 2: Expand to find leftmost and rightmost indices
Start=1, check left: nums[0]=2 !=4 stop; end=1, check right: nums[2]=4 matches, end=2; nums[3]=6 stop.Final Answer:
Option A -> Option AQuick Check:
Positions of 4 are indices 1 and 2 [OK]
Hint: Break binary search on first found, then expand boundaries [OK]
Common Mistakes:
- Off-by-one errors expanding boundaries
- Returning found index only
- Confusing indices with values
3. 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
4. What is the time complexity of counting occurrences of a target in a sorted array using binary search to find left and right boundaries?
medium
Solution
Step 1: Analyze binary search calls
Two binary searches are performed independently, each O(log n).Step 2: Combine complexities
Total time is O(log n) + O(log n) = O(log n), since constants are ignored.Final Answer:
Option B -> Option BQuick Check:
Two binary searches do not multiply complexities [OK]
Hint: Two O(log n) calls sum to O(log n) [OK]
Common Mistakes:
- Confusing two calls as O(log n * log n)
- Assuming linear scan is needed
- Believing built-in functions run in O(1)
5. Examine the following buggy code snippet for finding the smallest divisor. Which line contains the subtle bug that can cause incorrect results on some inputs?
medium
Solution
Step 1: Identify the purpose of is_feasible
It sums the ceiling division of each num by divisor to check if total ≤ threshold.Step 2: Spot the bug in division
Using integer division (//) underestimates the sum because it truncates instead of rounding up.Step 3: Consequence of the bug
Underestimating sum may cause the function to incorrectly return True, leading to wrong smallest divisor.Final Answer:
Option A -> Option AQuick Check:
Ceiling division needed, integer division breaks correctness -> [OK]
Hint: Ceiling division needed, integer division underestimates sum [OK]
Common Mistakes:
- Using integer division instead of ceiling
- Incorrect binary search boundaries
- Not early exiting feasibility check
