Data Structures & AlgorithmsHardCodeforces Div2 B · DP warm-up

Problem: What is the time complexity of the standard iterative binary search, and what precondition must hold for it to work correctly?

  1. A. O(log n) time; the array must be sorted (with respect to the search comparison)
  2. B. O(log n) time; works on any unsorted array too
  3. C. O(n) time; the array must be sorted
  4. D. O(1) time; no precondition needed

Correct answer: A. O(log n) time; the array must be sorted (with respect to the search comparison)

Binary search halves the search space each iteration (O(log n) iterations total), but it only works correctly when the array is sorted, since it relies on comparisons to eliminate half the remaining range.

More Coding Interview Problems questions

Exams that ask this topic