What is Binary Search?
Binary Search is a divide-and-conquer algorithm that repeatedly halves the search space. It reduces O(n) linear search to O(log n) by eliminating half the possibilities at each step.Quick Recognition: If you see “sorted array” or need to minimize/maximize something with a feasibility check, Binary Search is likely the answer!
Pattern Recognition Checklist
Use Binary Search When
- Array is sorted (or has monotonic property)
- Need O(log n) instead of O(n)
- Minimize/maximize with feasibility check
- Finding transition point where condition changes
- Search space can be halved each step
Don't Use When
- Array is unsorted with no order
- Need to find all occurrences
- Linear scan is already O(n) and sufficient
- No clear way to eliminate half
When to Use
Sorted Arrays
Finding elements, insertion points, or bounds
Monotonic Functions
Finding transition points where condition changes
Optimization Problems
Minimizing/maximizing values with feasibility check
Hidden Sorted Space
Rotated arrays, peak finding, matrix search
Pattern Variations
1. Standard Binary Search
Find exact target in sorted array.2. Lower Bound (First Occurrence)
Find first element greater than or equal to target.3. Upper Bound (Last Occurrence)
Find first element greater than target.4. Binary Search on Answer
When searching for optimal value, not index.Classic Problems
1. Search in Rotated Sorted Array
1. Search in Rotated Sorted Array
Problem: Search for target in rotated sorted array.Approach: Find which half is sorted, determine if target is in that half.Time: O(log n) | Space: O(1)
2. Find Peak Element
2. Find Peak Element
Problem: Find any peak element (greater than neighbors).Approach: Compare mid with mid+1, move towards the larger side.Time: O(log n) | Space: O(1)
3. Search 2D Matrix
3. Search 2D Matrix
Problem: Search in row-wise and column-wise sorted matrix.Approach: Treat as 1D array or start from top-right corner.Time: O(log(m*n)) or O(m+n) | Space: O(1)
4. Koko Eating Bananas
4. Koko Eating Bananas
Problem: Minimum eating speed to finish all bananas in h hours.Approach: Binary search on answer with feasibility check.Time: O(n log m) | Space: O(1)
5. Median of Two Sorted Arrays
5. Median of Two Sorted Arrays
Problem: Find median of two sorted arrays in O(log(m+n)).Approach: Binary search on partition point of smaller array.Time: O(log(min(m,n))) | Space: O(1)
Search in Rotated Sorted Array
Template Code
Common Mistakes
Debugging Checklist
When your Binary Search solution fails:1
Check Initial Bounds
Is
left = 0 and right = len - 1 or right = len?2
Check Loop Condition
Use
left <= right for exact match, left < right for bounds.3
Check Mid Calculation
Use
left + (right - left) / 2 to avoid overflow.4
Check Movement
Is
mid included or excluded? (right = mid vs right = mid - 1)5
Check Return Value
What should be returned when element is not found?
The Three Templates
Understanding which template to use is crucial:| Template | Loop Condition | Use Case | After Loop |
|---|---|---|---|
| Standard | left <= right | Find exact match | Return -1 if not found |
| Lower Bound | left < right | First >= target | left is answer |
| Upper Bound | left < right | First > target | left is answer |
Complexity Quick Reference
| Problem Type | Time | Space | Key Insight |
|---|---|---|---|
| Standard search | O(log n) | O(1) | Exact match |
| Lower/Upper bound | O(log n) | O(1) | First occurrence |
| Search on answer | O(n log range) | O(1) | Feasibility check |
| Rotated array | O(log n) | O(1) | Find sorted half |
| Peak finding | O(log n) | O(1) | Compare with neighbor |
Interview Problems by Company
- Easy
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Binary Search | All | Basic template |
| Search Insert Position | Amazon, Google | Lower bound |
| First Bad Version | Meta, Microsoft | Boolean search |
| Sqrt(x) | Amazon | Search on answer |
| Valid Perfect Square | Search on answer |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “Since the array is sorted, I can use Binary Search for O(log n) time.”
- “I’ll maintain two pointers, left and right, representing the search space.”
- “At each step, I calculate mid and compare with target.”
- “Based on comparison, I eliminate half the search space.”
- “I repeat until I find the target or the space is exhausted.”
When Interviewer Says...
When Interviewer Says...
| Interviewer Says | You Should Think |
|---|---|
| ”Array is sorted” | Binary Search! |
| ”Can you do better than O(n)?” | Binary Search might work |
| ”Find minimum/maximum that satisfies…” | Binary Search on answer |
| ”Array is rotated” | Modified Binary Search |
| ”Find first/last occurrence” | Lower/Upper bound |
Common Follow-ups
Common Follow-ups
Be ready for these follow-ups:
- “What if there are duplicates?” → Use lower/upper bound
- “What if array is rotated?” → Find sorted half first
- “Can you do it without extra space?” → Binary Search is already O(1) space
- “What about very large arrays?” → Binary Search scales well
Practice Problems
| Problem | Difficulty | Link |
|---|---|---|
| Binary Search | Easy | LeetCode 704 |
| Search Insert Position | Easy | LeetCode 35 |
| Search in Rotated Sorted Array | Medium | LeetCode 33 |
| Find Peak Element | Medium | LeetCode 162 |
| Median of Two Sorted Arrays | Hard | LeetCode 4 |
Practice Roadmap
1
Day 1: Standard Search
- Solve: Binary Search, Search Insert Position
- Focus: Master the basic template perfectly
2
Day 2: Bounds
- Solve: Find First and Last Position, First Bad Version
- Focus: Lower bound vs upper bound
3
Day 3: Modified Arrays
- Solve: Search in Rotated Array, Find Peak Element
- Focus: Finding the sorted half
4
Day 4: Search on Answer
- Solve: Koko Eating Bananas, Capacity To Ship
- Focus: Binary search on the result space