Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: A sorted array [1, 3, 3, 5, 8, 8, 8, 13, 21] is searched for the value 8 using the standard lower-bound binary search (find the first index whose value is ≥ 8, with the search range [lo, hi) starting at [0, 9)). Which index does the search return?

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

Correct answer: A. 4

Lower-bound search returns the first position with value ≥ x, so duplicates of 8 are skipped to the leftmost. Upper-bound search gives the index after the last 8 instead.

More Coding Interview Problems questions

Exams that ask this topic