What do you need before you can use binary search, and what are the usual implementation bugs?
Short answer
You need sorted data with O(1) index access; each step drops half, so it's O(log n).
3 points to say:
- 1 billion elements take only ~30 steps.
- Write
mid = lo + (hi - lo) / 2to avoid overflow. - Wrong bound updates loop forever.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.