🎯 Binary Search Variants
You've mastered Binary Search. Now it's time for the interview variants that appear constantly in FAANG interviews! These problems all use the same O(log n) binary search framework with clever modifications. Master these, and you'll ace binary search interview questions!
The Interview Favorites
5 Must-Know Variants
- First/Last Occurrence - Find boundaries in duplicates ⭐⭐⭐⭐⭐
- Rotated Sorted Array - Search in rotated array ⭐⭐⭐⭐⭐
- Peak Element - Find local maximum ⭐⭐⭐⭐
- Insert Position - Where to insert to maintain order ⭐⭐⭐
- Square Root - Binary search on answer space ⭐⭐⭐
All use O(log n) binary search!
Variant 1: First and Last Occurrence
Problem: Find the first and last position of a target in a sorted array with duplicates.
Example
Array: [1, 2, 2, 2, 2, 3, 4], Target: 2
First occurrence: index 1
Last occurrence: index 4
Count: 4 occurrences
The Trick
Keep searching after finding!
- First occurrence: When found, search LEFT (right = mid - 1)
- Last occurrence: When found, search RIGHT (left = mid + 1)
- Count: last - first + 1 in O(log n)!
Variant 2: Rotated Sorted Array ⭐ Most Common!
Problem: Search in a sorted array that has been rotated.
Example
Original: [0, 1, 2, 4, 5, 6, 7]
Rotated: [4, 5, 6, 7, 0, 1, 2] (rotated at index 4)
Search for 0 → index 4
See It in Action
Watch how we detect which half is sorted:
✅ The Key Insight
One half is ALWAYS sorted!
- Compare
arr[left]witharr[mid] - If
arr[left] ≤ arr[mid]: Left half is sorted - Otherwise: Right half is sorted
- Check if target is in the sorted half
- If yes, search sorted half; if no, search other half
Still O(log n)!
The Code
Click Run to execute your code
Variant 3: Peak Element
Problem: Find a peak element (greater than its neighbors).
Example
Array: [1, 3, 20, 4, 1, 0]
Peak: 20 at index 2 (20 > 3 and 20 > 4)
The Strategy
Compare arr[mid] with arr[mid + 1]:
- If
arr[mid] < arr[mid + 1]: Ascending, peak is RIGHT - If
arr[mid] > arr[mid + 1]: Descending, peak is LEFT or mid
Always converges to a peak!
Variant 4: Insert Position
Problem: Find the index where target should be inserted to maintain sorted order.
Example
Array: [1, 3, 5, 6], Target: 2
Insert at index 1 → [1, 2, 3, 5, 6]
The Trick
Return the left pointer when not found!
After binary search completes without finding target, left
points to the correct insert position.
Variant 5: Square Root
Problem: Find integer square root without using sqrt().
Example
sqrt(8) = 2 (since 2² = 4 ≤ 8 < 3²=9)
sqrt(16) = 4 (perfect square)
Binary Search on Answer Space!
Instead of searching in an array, search in the range [1, n/2]:
- If
mid² == n: Perfect square! - If
mid² < n: Answer might be mid, search right - If
mid² > n: Too large, search left
Same O(log n) approach, different search space!
Interview Tips
✅ How to Ace Binary Search Variants
- Recognize the pattern: Can I eliminate half each step?
- Start with template: Use standard binary search framework
- Modify condition: Adjust the comparison/direction logic
- Test edge cases: Empty array, single element, duplicates
- Explain complexity: Always mention O(log n)
⚠️ Common Mistakes
- Rotated array: Forgetting to check which half is sorted
- First/Last: Stopping search after first match
- Peak element: Not handling array boundaries
- All variants: Off-by-one errors in loop condition
Complexity Analysis
| Variant | Time | Space | Difficulty |
|---|---|---|---|
| First/Last Occurrence | O(log n) | O(1) | Medium |
| Rotated Sorted Array | O(log n) | O(1) | Medium-Hard |
| Peak Element | O(log n) | O(1) | Medium |
| Insert Position | O(log n) | O(1) | Easy-Medium |
| Square Root | O(log n) | O(1) | Medium |
💡 Key Takeaway
Binary Search Variants are interview favorites:
- ✅ All use O(log n) binary search framework
- ✅ Modify condition/direction logic for each variant
- ✅ Rotated array: Most common in FAANG interviews!
- ✅ First/Last: Essential for counting in sorted arrays
- ✅ Practice these - they appear constantly!
Master the template, adapt for each problem!
Summary
The Binary Search Variants Guide
- Framework: All use standard binary search with modifications
- Complexity: All are O(log n) - same as basic binary search
- Interview: These appear constantly in FAANG interviews!
Interview tip: When you see a sorted array problem, think binary search! For rotated arrays, remember one half is always sorted. For first/last, keep searching after finding. Practice these variants - they're interview gold!
What's Next?
You've mastered binary search and its variants! Next: Interpolation Search - an optimization for uniformly distributed data.
Enjoying these tutorials?