Search in a Rotated Sorted Array with Duplicates
Reported by candidates from ByteDance's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The duplicate-boundary case is what kills most first attempts at this ByteDance problem, reported in August 2026. It looks like standard rotated array search, but you must return the lowest index of target, not just any hit. That twist trips people who copy the textbook version. It's binary search with a nasty fallback when nums[lo], nums[mid] and nums[hi] are all equal. If you blank on that branch during the OA, StealthCoder is a safety net that runs invisibly and reads the problem for you. But you can learn the trick tonight.
The problem
Given an integer array nums that was sorted in nondecreasing order and then rotated at an unknown pivot, return the lowest array index whose value equals target. The array may contain duplicate values. If target does not occur, return -1. Your algorithm should use the sorted, rotated structure to discard unambiguous regions. Because duplicate boundary values can hide the sorted side, the worst case may require examining every element. Function search(nums: int[], target: int) → int Examples Example 1 nums = [2,5,6,0,0,1,2] target = 0 return = 3 The target occurs at indices 3 and 4, so the lowest matching index is 3. Example 2 nums = [1,1,3,1] target = 3 return = 2 The array is a rotation of a nondecreasing array and the only 3 is at index 2. Example 3 nums = [4,4,5,1,2,4] target = 3 return = -1 The target does not occur, so the result is -1. Constraints 1 <= nums.length <= 100000. -2147483648 <= nums[i], target <= 2147483647. Before rotation, nums was sorted in nondecreasing order. nums may contain duplicate values. The array was rotated at an arbitrary pivot, including a rotation by zero positions.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The core move is modified binary search. At each step, figure out which half is sorted by comparing nums[lo] and nums[mid]. If target falls inside the sorted half, go there. Otherwise go the other way. Duplicates break this. When nums[lo] == nums[mid] == nums[hi], you can't tell which side is sorted, so shrink both ends by one. That's why worst case is O(n). The second pitfall is the lowest index requirement. Finding a match at mid isn't enough, since an earlier copy may sit to the left. Either keep searching left after a hit and record the best index, or find the pivot and do a lower-bound search on each sorted segment. Check Example 2, [1,1,3,1], where the equal-ends case actually fires. If you freeze on that branch in the live OA, StealthCoder is your hedge.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Search in a Rotated Sorted Array with Duplicates cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as search in rotated sorted array ii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass ByteDance's OA.
ByteDance reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Search in a Rotated Sorted Array with Duplicates FAQ
What's the trick in this ByteDance rotated array problem?+
Binary search where you identify the sorted half each step. The catch is duplicates. When the low, mid and high values are equal, you can't decide a side, so you move lo up and hi down by one and continue. That's the whole difference from the no-duplicates version.
Why does the worst case become O(n)?+
An array like all 1s with a single different value hides the pivot. Equal boundary values give no information, so you can only discard one element per step. The problem statement even warns that you may have to examine every element.
How do I make sure I return the lowest index?+
Don't return on the first match. Record the index, then keep searching the left side for an earlier copy. Or locate the pivot first and run a lower-bound binary search on each sorted segment, then take the smaller valid index.
Which edge cases should I test before submitting?+
Test rotation by zero, a single element array, a missing target returning -1, and duplicates on both ends like [1,1,3,1]. Also test [2,5,6,0,0,1,2] with target 0, where the answer must be 3 and not 4.
How do I prepare for this in 48 hours?+
Write the no-duplicates rotated search from memory, then add the shrink-both-ends branch for equal values. Then add the lowest-index logic. Run the three given examples by hand. Two focused hours covers it, since it's one pattern with two small modifications.