Data Structures & AlgorithmsEasyInfosys SP/DSE · Product Company SDE

Problem: Find the minimum of a rotated sorted array of distinct values.

Problem: Find the minimum of a rotated sorted array of distinct values.
def find_min(a):
    lo, hi = 0, len(a) - 1
    while lo < hi:
        mid = (lo + hi) // 2
        if a[mid] > a[hi]:
            lo = mid + 1
        else:
            hi = mid
    return a[lo]
print(find_min([4, 5, 6, 7, 0, 1, 2]), find_min([11, 13, 15, 17]), find_min([2, 1]))
What is printed?
  1. A. 0 11 1
  2. B. 0 17 1
  3. C. 0 11 2
  4. D. 1 11 1

Correct answer: A. 0 11 1

Compare with the right end: if a[mid] > a[hi] the drop is to the right of mid, else the minimum is at mid or to its left. A non-rotated array keeps moving hi left to index 0. O(log n). Comparing with a[lo] instead fails on the non-rotated case.

More Coding Interview Problems questions

Exams that ask this topic