Data Structures & AlgorithmsVery hardProduct-company MCQ pattern

Binary search for first/last occurrence runs on the sorted array [1, 3, 7, 9, 11, 13, 15, 15, 16, 17, 18] for target 15. What (first index, last index) pair (0-based) does it return?

  1. A. (6, 6)
  2. B. (6, 8)
  3. C. (6, 7)
  4. D. (5, 7)

Correct answer: C. (6, 7)

The first-occurrence search keeps moving left after a match (hi = mid − 1) to find the earliest index, and the last-occurrence search keeps moving right (lo = mid + 1) to find the latest; for 15 in this array that gives first index 6 and last index 7.

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

Exams that ask this topic