Binary Search
🔍 Binary Search
Section titled “🔍 Binary Search”Welcome to the Binary Search section. This module covers everything from the core algorithm to advanced applications and interview-ready patterns.
🎯 What is Binary Search?
Section titled “🎯 What is Binary Search?”Binary search is a divide-and-conquer search algorithm that finds a target in a sorted collection by repeatedly halving the search space.
Instead of checking every element one-by-one (O(n)), binary search eliminates half of the remaining candidates on every step — achieving O(log n) time.
Searching for 35 in a sorted array of 1,000,000 elements:
Linear Search: up to 1,000,000 comparisonsBinary Search: at most 20 comparisons (log₂ 1,000,000 ≈ 20)That is the power of logarithmic time.
📖 Learning Path
Section titled “📖 Learning Path”🎯 Introduction
Section titled “🎯 Introduction”- Binary Search — Introduction — Algorithm walkthrough, iterative vs recursive, off-by-one bugs
🔹 5 Core Patterns
Section titled “🔹 5 Core Patterns”- Patterns Overview — Decision flowchart to pick the right pattern
- Classic Search — Find exact value in sorted array
- First/Last Occurrence — Handle duplicates
- Search on Answer — Min/max feasible value
- Rotated Array — Search in rotated sorted array
- Monotonic Function — Boundary search
💻 10 Classic Problems
Section titled “💻 10 Classic Problems”- Problems Overview — 10 problems from easy to hard
- Search Rotated Sorted Array, Find Min Rotated, 2D Matrix, Peak Element, Koko Bananas, Ship Packages, Split Array, Sqrt(x), First Bad Version, Time-Based KV
🚀 Advanced
Section titled “🚀 Advanced”- Advanced Overview — Beyond the basics
- Floating-Point Search — Continuous ranges with precision
- Median of Two Arrays — The hardest BS problem
- Exponential/Interpolation — When log n isn’t enough
- Real-World Problems — Painter, Cows, Force, Trips
🧠 Interview Prep
Section titled “🧠 Interview Prep”- Interview Questions Overview — 12+ questions
📚 Recommended Learning Path
Section titled “📚 Recommended Learning Path”| Step | Focus | Topic |
|---|---|---|
| 1 | Understand | Introduction — algorithm walkthrough and complexity |
| 2 | Learn Patterns | 5 Patterns — templates for 95% of problems |
| 3 | Practice | 10 Problems — apply patterns to classic problems |
| 4 | Deepen | Advanced — floating-point, median, exponential |
| 5 | Prepare | Interview Questions — nail the verbal explanations |
🧩 When Does Binary Search Apply?
Section titled “🧩 When Does Binary Search Apply?”| Scenario | Example |
|---|---|
| Sorted array | Find a value, find first/last occurrence |
| Rotated sorted array | Find value after rotation |
| Monotonic function | Find where f(x) crosses a threshold |
| Answer space | ”Find minimum X such that condition(X) is true” |
| 2D matrix with sorted rows | Search in row-major sorted grid |
| Floating-point precision | Find square root, cube root |
The golden rule: If you can say “I can eliminate half the candidates” after one comparison, binary search applies.
⚡ Quick Complexity Reference
Section titled “⚡ Quick Complexity Reference”| Operation | Time | Space |
|---|---|---|
| Classic binary search | O(log n) | O(1) iterative / O(log n) recursive |
| Find first / last occurrence | O(log n) | O(1) |
| Binary search on answer | O(log(range) × cost of check) | O(1) |
| Exponential search | O(log n) | O(1) |