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

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

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

Correct answer: D. 1

Simulate: lo=0, hi=39; each probe halves the range; 59 sits at index 19 and is found on probe 1.

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

Exams that ask this topic