Tech Interview RoomStudy guides

Binary Search

Search in a rotated sorted array. Compare linear search, find-pivot-then-search, and one-pass binary search.

Intermediate · 2 min read · Lesson 2 of 3 in Binary Search

Problem

A sorted array rotated at an unknown pivot: [4, 5, 6, 7, 0, 1, 2], target 0 → index 4. Distinct values. Required O(log n).

Pattern

Binary search on a partially sorted range: at every step at least one half (lo..mid or mid..hi) is sorted; check whether the target lies in the sorted half and discard the other.

Approach 1: Linear scan (feasible, O(n))

java
class RotatedLinear {
    static int search(int[] a, int target) {
        for (int i = 0; i < a.length; i++) if (a[i] == target) return i;
        return -1;
    }
}

Approach 2: Find the rotation point, then a normal binary search (O(log n), two passes)

java
import java.util.Arrays;

class RotatedPivot {
    static int minIndex(int[] a) {                               // index of the smallest element
        int lo = 0, hi = a.length - 1;
        while (lo < hi) {
            int mid = lo + (hi - lo) / 2;
            if (a[mid] > a[hi]) lo = mid + 1; else hi = mid;
        }
        return lo;
    }
    static int search(int[] a, int target) {
        int p = minIndex(a);
        int[] range = target >= a[p] && target <= a[a.length - 1] ? new int[] {p, a.length} : new int[] {0, p};
        int i = Arrays.binarySearch(a, range[0], range[1], target);
        return i >= 0 ? i : -1;
    }
}

Approach 3: One-pass modified binary search (O(log n)) — optimal

java
class RotatedOnePass {
    static int search(int[] a, int target) {
        int lo = 0, hi = a.length - 1;
        while (lo <= hi) {
            int mid = lo + (hi - lo) / 2;
            if (a[mid] == target) return mid;
            if (a[lo] <= a[mid]) {                               // left half sorted
                if (target >= a[lo] && target < a[mid]) hi = mid - 1; else lo = mid + 1;
            } else {                                             // right half sorted
                if (target > a[mid] && target <= a[hi]) lo = mid + 1; else hi = mid - 1;
            }
        }
        return -1;
    }
}

class RotatedDemo {
    public static void main(String[] args) {
        int[] a = {4, 5, 6, 7, 0, 1, 2};
        for (int t : new int[] {0, 4, 7, 3}) {
            System.out.println(t + ": " + RotatedLinear.search(a, t) + " " + RotatedPivot.search(a, t) + " " + RotatedOnePass.search(a, t));
        }
        System.out.println("min = " + a[RotatedPivot.minIndex(a)]);   // 0
    }
}

Feasible → optimal

O(n) linear → O(log n) with two binary searches → O(log n) in a single pass. With duplicates ([1, 0, 1, 1, 1]), you cannot always tell which half is sorted, so shrink with lo++/hi-- when a[lo] == a[mid] == a[hi]: worst case O(n).

Follow-up questions interviewers ask

Try answering each one before you open it.

Related (same pattern): find the minimum in a rotated sorted array.

Compare a[mid] with a[hi]: if bigger, the minimum is to the right (lo = mid + 1), else it is at mid or left (hi = mid). O(log n), as minIndex above.

Related (same pattern): find a peak element.

If a[mid] < a[mid + 1] a peak exists to the right, otherwise at mid or left. Binary search on the slope direction gives O(log n) even though the array is not sorted.