Data Structures & AlgorithmsHardGoogle · Flipkart interview

Search for a value in a sorted array that was rotated and MAY CONTAIN DUPLICATES (e.g. [1, 1, 1, 1, 0, 1, 1]). What is the worst-case time of the best comparison-based algorithm?

  1. A. O(log n), the same as without duplicates
  2. B. O(log² n), by binary searching for the pivot first
  3. C. O(n), because when a[lo] = a[mid] = a[hi] the half holding the target cannot be identified
  4. D. O(√n), by probing blocks of size √n

Correct answer: C. O(n), because when a[lo] = a[mid] = a[hi] the half holding the target cannot be identified

With all equal values except one hidden element, every probe reveals nothing about where the odd one is, so any algorithm must inspect Ω(n) positions in the worst case. The usual fix (lo += 1, hi −= 1 when the ends equal mid) is therefore linear in the worst case.

More Coding Interview Problems questions

Exams that ask this topic