Lesson

Binary Search

Halve a monotonic search space each step — O(log n) probes.

Easy → HardTime O(log n)Space O(1)
Practice problems
Loading visualization…

Loading solution…

Classic form

Keep lo..hi inclusive. Compare mid to target. Discard the half that cannot contain the answer. Exit when interval empties.

Answer-space BS

When the array isn't sorted but a feasibility function is monotonic (can finish in X days?), binary search on X.

Studio

Binary Search studio shows lo/hi/mid and discarded halves lighting up each probe.

Pitfalls

  • !Overflow mid = (lo+hi)/2
  • !Wrong inclusive/exclusive bounds
  • !Infinite loop when mid doesn't move

Practice from this topic

Showing 24 of the related problems in the 1000-question bank.

View all →

Got the lesson?

Mark Binary Search as understood when the ideas stick.