Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: The binary search for a value in the sorted array [2, 5, 8, 12, 16, 23, 38, 56, 72, 91] is run with the standard midpoint mid = (lo + hi) // 2 and searching for 23 over the inclusive range [0, 9]. How many midpoints are examined before the value is found (counting the first)?

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

Correct answer: D. 3

Each iteration examines one midpoint and halves the range. Forgetting to count the first midpoint or the final match undercounts by one.

More Coding Interview Problems questions

Exams that ask this topic