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

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

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

Correct answer: B. 3

Simulate: lo=0, hi=18; each probe halves the range; 20 sits at index 6 and is found on probe 3.

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

Exams that ask this topic