Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA

Problem: A sorted list 2, 4, 6, 8, 10, 12, 14 is treated as a rotated array: the first 3 values are moved to the end, giving 8, 10, 12, 14, 2, 4, 6 (the rotation is known). Searching for the value 4 with a rotated-array binary search, how many probes (midpoints examined, counting the first) are needed before the value is found, starting with lo = 0 and hi = 6?

  1. A. 2
  2. B. 3
  3. C. 1
  4. D. 4

Correct answer: A. 2

In a rotated array one half is always sorted, so the search decides which half contains the target. Using plain binary search on the unrotated order mis-directs the search.

More Coding Interview Problems questions

Exams that ask this topic