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?
- A. O(log n) time; the array must be sorted (with respect to the search comparison)
- B. O(log n) time; works on any unsorted array too
- C. O(n) time; the array must be sorted
- 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
- Problem: Which best describes why DFS-based topological sort (using finish times, pushing to a stack) correctly produces a topological…
- Problem: A problem requires finding all pairs in an array that sum to a target, allowing duplicate pairs to be counted once. Which is the…
- Problem: Which statement about the time complexity of building a suffix array naively (sorting all n suffixes with standard string…
- Problem: A delivery app logs the parcel weights of a day in an array. Return the indices of the two parcels whose weights add up exactly…
- Problem: Given an unsorted array of n ≤ 10⁵ integers and a target, report whether two different positions hold values summing to the…
- Problem: A stock tracker gets daily prices and must report the best profit from one buy followed by one later sell (0 if no profit is…