The Search in Rotated Sorted Array II interview question extends the first version by allowing duplicate values in the rotated sorted array. Given such an array and a target, return true if the target exists. Duplicates make it impossible to always determine which half is sorted, requiring a fallback that degrades worst-case complexity to O(n) but maintains O(log n) average case.
Apple, Cisco, Uber, Microsoft, Meta, Amazon, Google, Bloomberg, and Adobe ask this problem because it tests whether candidates understand the limitation duplicates impose on binary search correctness. The fallback (left++) when nums[left] == nums[mid] == nums[right] is the key insight. This models real-world scenarios where data streams may have repeated timestamps or sorted logs with duplicate entries.
The pattern is modified binary search with a duplicate-handling fallback. The algorithm mirrors Search in Rotated Sorted Array I, but adds: when nums[left] == nums[mid], we cannot determine which half is sorted. In this case, increment left (safely discard one duplicate from the left) and continue. This worst-case O(n) step is necessary only when the leftmost element equals the mid element.
Array: [2, 5, 6, 0, 0, 1, 2], target = 0.
true.Duplicate fallback: [1,1,1,3,1], target=3.
left++ is safe.[1,1,1,1,1,1] where the fallback is triggered at every step.For the Search in Rotated Sorted Array II coding problem, the binary search array interview pattern extends with one key addition: handle nums[left] == nums[mid] by incrementing left. Know that this makes worst-case O(n) — be ready to explain this tradeoff to interviewers at Glassdoor and Walmart Labs. The distinction between versions I and II is often the follow-up: "what changes when duplicates are allowed?"
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Peak Index in a Mountain Array | Medium | Solve | |
| Find Minimum in Rotated Sorted Array | Medium | Solve | |
| Maximum Candies Allocated to K Children | Medium | Solve | |
| Minimum Limit of Balls in a Bag | Medium | Solve | |
| Minimum Number of Days to Make m Bouquets | Medium | Solve |