Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft

For the sorted array [1, 3, 4, 6, 6, 9, 9, 11, 13, 14, 14, 15] (0-based), what are lower_bound(14) (first index with value ≥ 14) and upper_bound(14) (first index with value > 14), and hence how many times does 14 occur?

  1. A. 9 and 12; count 3
  2. B. 9 and 10; count 1
  3. C. 9 and 11; count 2
  4. D. 10 and 11; count 1

Correct answer: C. 9 and 11; count 2

lower_bound is the first 14 at index 9; upper_bound is one past the last 14, index 11. Their difference 2 is the count — both found by O(log n) binary searches.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic