Search in Rotated Sorted Array
Reported by candidates from ByteDance's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The O(log n) requirement is the whole point of this ByteDance OA question, reported in July 2025. You get a sorted array of distinct values that may be rotated at an unknown pivot, and you return the index of the target or -1. A linear scan passes the examples and fails the brief. It's a binary search with one extra decision per step. If you blank on that decision during the live OA, StealthCoder runs invisibly as a safety net and gives you the working solution while you keep your cool.
The problem
You are given an integer array nums that was originally sorted in strictly increasing order and then possibly rotated at an unknown pivot. All values in nums are distinct. Given an integer target, return its index in nums. Return -1 when target is absent. Your algorithm must run in O(log n) time. Function search(nums: int[], target: int) → int Examples Example 1 nums = [4,5,6,7,0,1,2] target = 0 return = 4 The value 0 appears at index 4. Example 2 nums = [4,5,6,7,0,1,2] target = 3 return = -1 The value 3 does not appear in the array. Example 3 nums = [1] target = 0 return = -1 The one-element array contains 1, not 0. Constraints 1 <= nums.length <= 100000 -10^9 <= nums[i], target <= 10^9 All values in nums are distinct. nums is a rotation of a strictly increasing array.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: at every step, at least one half of the current range is fully sorted. Compare nums[lo] to nums[mid]. If nums[lo] <= nums[mid], the left half is sorted. Check whether target sits between nums[lo] and nums[mid]. If yes, move hi to mid-1. If no, move lo to mid+1. Otherwise the right half is sorted, so run the mirror check. The common pitfall is the boundary comparison. Use <= on nums[lo] <= nums[mid] so a two-element range works, and keep the target checks inclusive on the sorted side. Distinct values means you don't need duplicate handling. Another mistake is finding the pivot first and then searching, which works but adds code and bugs. One pass is cleaner. If the logic tangles under pressure, StealthCoder is the hedge for the live OA. It reads the problem and hands you the template so you can verify it against the examples.
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 Rotated Sorted Array 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. 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 Rotated Sorted Array FAQ
How hard is Search in Rotated Sorted Array really?+
Medium. The idea is short, but the boundary conditions trip people up. If you can write plain binary search from memory, you're one insight away: one half is always sorted. Most failures come from off-by-one errors in the range checks, not from the concept.
What's the trick to getting O(log n)?+
Don't search for the pivot separately. At each mid, figure out which half is sorted by comparing nums[lo] and nums[mid]. Then test whether the target falls inside that sorted half's bounds. Keep that half or discard it. Each step halves the range.
Which edge cases should I test before submitting?+
Test a one-element array with a match and a miss. Test an array that isn't rotated at all. Test a two-element rotated array like [3,1] with target 1. Test the target at the first and last positions. Test a target that's absent, which must return -1.
Is this pattern still asked at ByteDance?+
It was reported in July 2025, so yes, it's current. Rotated-array binary search is a staple because it checks whether you can adapt a known algorithm instead of reciting it. Expect variants like finding the minimum or handling duplicates.
How do I prepare in 48 hours?+
Write the solution from scratch three times, without looking. Trace it by hand on [4,5,6,7,0,1,2] with targets 0 and 3. Then write the find-minimum variant. Focus on why each pointer moves, not on memorizing code. That covers most of what this question can throw at you.