intro practice · coding problem
Binary Search
LeetCode #792 — Binary Search. Solve on LeetCode, then implement here if you want it in your drill queue. https://leetcode.com/problems/binary-search/
The mechanism
Binary search is not about sorted arrays — it is about a monotonic predicate. If you can phrase the answer as "the smallest x for which condition(x) is true", you can binary-search the answer space, halving it each step.
Expected evidence
Pass all LeetCode test cases for this problem.
Try this drill in the practice workspace →
Hints when you need them
- Pattern hint: binary search.
- Start with brute force, then optimize time/space.
- Write down invariants before coding.
Watch for these mistakes
- Infinite loops from a wrong mid update or loop condition
- Searching values when you should search the answer space
- Integer overflow in `(lo + hi) / 2` — use `lo + (hi - lo) / 2`