Data Structures & AlgorithmsHard

In the sorted array 1 2 2 2 3 5 5 8, what is the 0-based index of the FIRST occurrence of the value 5 (the leftmost position where 5 appears)?

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

Correct answer: C. 5

The leftmost occurrence is found by the lower-bound binary search: keep searching left whenever the middle value equals the key. Taking the last occurrence, one past the first, or a different value gives other indices.

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

Exams that ask this topic