Search in Rotated Sorted Array: Optimal O(log n) Solution (Data-Backed Guide)

By DSA Prep Team · March 4, 2026 · 9 min read · Algorithms

Search in Rotated Sorted Array is the #12 most asked LeetCode problem globally — the definitive test of whether you truly understand binary search beyond its textbook form.

Our analysis of 10,385 verified interview questions across 259 companies shows it appeared 68 times across 26 companies — including Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, LinkedIn, ByteDance, and Arista Networks.

Standard binary search requires a fully sorted array. This problem gives you a sorted array that has been rotated — and still demands O(log n). The key insight: even in a rotated array, at least one half of any binary search window is always sorted. That's enough to make binary search work.

Table of Contents

The Data: 26 Companies Ask This

MetricValue
Total Interview Appearances68 verified mentions
Companies That Ask It26 companies
Global Rank#12 out of 10,385 questions
DifficultyMedium
Core PatternsArray, Binary Search

📌 Companies That Ask This Problem

Google, Amazon, Meta, Microsoft, Goldman Sachs, Bloomberg, LinkedIn, ByteDance, Arista Networks, Adobe, Anduril, Apple, Autodesk, Criteo, Disney, Flipkart, Grammarly, Infosys, Media.net, MongoDB, and 6 more.

Problem Statement

LeetCode #33 — Search in Rotated Sorted Array

There is an integer array nums sorted in ascending order (with distinct values). Prior to being passed, nums is possibly rotated at an unknown pivot index. Given nums and an integer target, return the index of target if it exists, or -1.

Must run in O(log n) time.

Examples
Input:  nums = [4,5,6,7,0,1,2], target = 0   →  Output: 4
Input:  nums = [4,5,6,7,0,1,2], target = 3   →  Output: -1
Input:  nums = [1], target = 0                →  Output: -1

The Core Intuition: One Half is Always Sorted

In a rotated array, when you pick a midpoint, exactly one of the two halves (left or right of mid) is fully sorted. You can determine which one by comparing nums[left] with nums[mid].

💡 The Decision Tree in One Rule

Identify the sorted half. Check if target is in the sorted half's range. If yes — search there. If no — search the other half. Repeat.

The Algorithm: Modified Binary Search

Visual Trace — nums = [4,5,6,7,0,1,2], target = 0
left=0, right=6, mid=3 → nums[mid]=7
  nums[left]=4 <= nums[mid]=7 → left half [4,5,6,7] is sorted
  target=0 not in [4,7] → search right: left=4

left=4, right=6, mid=5 → nums[mid]=1
  nums[left]=0 <= nums[mid]=1 → left half [0,1] is sorted
  target=0 in [0,1] → search left: right=4

left=4, right=4, mid=4 → nums[mid]=0 == target → return 4 ✅

Code in Python, Java & JavaScript

Python 3
def search(nums: list[int], target: int) -> int:
    left, right = 0, len(nums) - 1

    while left <= right:
        mid = (left + right) // 2

        if nums[mid] == target:
            return mid

        # Left half is sorted
        if nums[left] <= nums[mid]:
            if nums[left] <= target < nums[mid]:
                right = mid - 1  # target in left half
            else:
                left = mid + 1   # target in right half
        # Right half is sorted
        else:
            if nums[mid] < target <= nums[right]:
                left = mid + 1   # target in right half
            else:
                right = mid - 1  # target in left half

    return -1
Java
class Solution {
    public int search(int[] nums, int target) {
        int left = 0, right = nums.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (nums[mid] == target) return mid;

            if (nums[left] <= nums[mid]) {  // left half sorted
                if (nums[left] <= target && target < nums[mid])
                    right = mid - 1;
                else
                    left = mid + 1;
            } else {                         // right half sorted
                if (nums[mid] < target && target <= nums[right])
                    left = mid + 1;
                else
                    right = mid - 1;
            }
        }
        return -1;
    }
}
JavaScript
var search = function(nums, target) {
    let left = 0, right = nums.length - 1;

    while (left <= right) {
        const mid = Math.floor((left + right) / 2);

        if (nums[mid] === target) return mid;

        if (nums[left] <= nums[mid]) {  // left half sorted
            if (nums[left] <= target && target < nums[mid])
                right = mid - 1;
            else
                left = mid + 1;
        } else {                         // right half sorted
            if (nums[mid] < target && target <= nums[right])
                left = mid + 1;
            else
                right = mid - 1;
        }
    }
    return -1;
};

Complexity Analysis

📊 Complexity

Rotated Array Family

ProblemKey Difference
LC #33 Search (distinct values)Base problem — one half always sorted
LC #81 Search with DuplicatesWhen nums[left]==nums[mid], can't determine sorted half → shrink left
LC #153 Find MinimumFind pivot — minimum is where sorted order breaks
LC #154 Find Min with DuplicatesSame, but handle nums[left]==nums[mid]==nums[right] edge case

How to Actually Remember This

✅ Your Spaced Repetition Schedule

  1. Today: Solve LC #33. Then LC #153 (Find Minimum in Rotated Array) — simpler variant, find pivot.
  2. Day 3: Solve LC #81 (with duplicates) — handle the ambiguous case when left==mid.
  3. Day 7: Re-implement LC #33 from scratch. Draw the decision tree first.
  4. Day 14: Solve LC #154 (Find Minimum II with duplicates).

Tag on DSAPrep.dev under Binary Search.

Track on DSAPrep.dev →