TRT
Sign in
mediumCorecoding

Search in a rotated sorted array

Find a target in O(log n) in a sorted array that was rotated, and explain the property that makes it possible.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstRotated and two-dimensional dataPro — members onlyBinary search still works when sorted data has been rotated, because one half of any range is always sorted. Sorted grids can be searched as one long array, or with a staircase walk from a corner.

A strong answer

A strong candidate frames Search in a rotated sorted array around the exact memory layout, compiler semantics, and hardware execution model covered in Rotated and two-dimensional data. Find a target in O(log n) in a sorted array that was rotated, and explain the property that makes it possible.

Binary search needs to know, after one comparison, which half can't contain the target. A fully sorted array gives that for free. This lesson looks at data that is only partly sorted, where a little more reasoning per step recovers the same O(log n).

A rotated sorted array is a sorted array that has been cut at some point and had its two pieces swapped: [0, 1, 2, 4, 5, 6, 7] rotated by four becomes [4, 5, 6, 7, 0, 1, 2]. Such arrays appear when a circular buffer is read from an arbitrary start, or when sorted data is appended to the end of older sorted data.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Search a rotated sorted array in O(log n) by finding the sorted half at each step.
  • Mastery of core invariant: Find the rotation point, the minimum, with a single comparison per step.
  • Mastery of core invariant: Search sorted matrices with flat-index binary search or a staircase walk, and know which applies.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01How would you find the index of the minimum instead?
  2. 02What changes if the array can contain duplicates?
  3. 03Could you first find the rotation point and then run a normal binary search? Compare the two approaches.
  4. 04How many times was the array rotated?

Not started

Sign in to save your learning progress.

Sign in to save