Search in Rotated Sorted Array
Problem
Take an ascending array of distinct integers and cut it at some position you are not told, then swap the two pieces: [2, 3, 6, 12, 15, 18] cut before the 15 becomes [15, 18, 2, 3, 6, 12]. Given the rotated array nums and an integer target, return the index of target in nums, or -1 if it is absent. The solution should take logarithmic time.
numshas between 1 and 8,000 elements, all distinct.- Values and
targetare integers between -1,000,000 and 1,000,000. - The cut may be at position zero, in which case the array is simply sorted.
Examples
nums = [15, 18, 2, 3, 6, 12], target = 6→4.nums = [15, 18, 2, 3, 6, 12], target = 10→-1; 10 would sit between 6 and 12 in the sorted order, and nothing is there.nums = [7, 3], target = 3→1. Tiny inputs are where the half-selection logic usually breaks.
Hints
- Plain binary search fails because comparing
targettonums[mid]no longer tells you which side to go. What extra comparison would restore that information? - Split the array at
mid. Can both halves be "broken" by the rotation at the same time? - Exactly one half is sorted. Check whether
targetlies inside that sorted half's value range; if yes go there, otherwise go to the other half.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.