Data Structures & AlgorithmsModerateInfosys · Cognizant pattern

Binary search with mid = (lo + hi) / 2 (integer division) looks for 31 in [3, 9, 14, 20, 27, 31, 40] (0-based). Which indices are probed, in order?

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

Correct answer: C. 3, 5

lo = 0, hi = 6 → mid 3 (20 < 31) → lo = 4. mid = (4 + 6)/2 = 5 → a[5] = 31, found. Two probes: 3, 5. Probing 4 next would be linear scanning, not halving.

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

Exams that ask this topic