Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE

A sorted array holds the 31 values 2, 5, 8, … (step 3). Binary search with mid = ⌊(lo+hi)/2⌋ and 0-based indices looks for 17. How many elements are compared with 17 until it is found?

  1. A. 5
  2. B. 12
  3. C. 8
  4. D. 4

Correct answer: D. 4

Simulate: lo=0, hi=30; each probe halves the range; 17 sits at index 5 and is found on probe 4.

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

Exams that ask this topic