Data Structures & AlgorithmsVery hardGATE CS · TCS NQT

A binary search on the sorted array 1, 2, ..., 100 looks for 37 using mid = (lo + hi) // 2 with lo = 0 and hi = 99. How many comparisons with array elements are made, counting the comparison that finds the target?

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

Correct answer: D. 3

The mids are 49, 24 and 36 (values 50, 25 and 37), so the target is found on the third comparison.

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

Exams that ask this topic