Data Structures & AlgorithmsModerateTCS NQT · binary search

Problem: What is the expected number of comparisons for a binary search on a sorted array of n = 1000 elements (successful search, worst case)?

  1. A. 1000 comparisons
  2. B. 500 comparisons
  3. C. ceil(log2(1001)) = 10 comparisons in the worst case
  4. D. log2(1000) rounded down to 9

Correct answer: C. ceil(log2(1001)) = 10 comparisons in the worst case

The worst case is about log2(n) + 1 comparisons, which is 10 for n = 1000.

More Coding Interview Problems questions

Exams that ask this topic