Binary Search Cheat Sheet
Quick reference for coding interviews. Bookmark this page!
What is Binary Search?
Divide and conquer with logarithmic time complexity.
Trigger Words
When you see these in a problem, think Binary Search:
Template Code
def search(nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
Complexity
| Typical Time | O(log n) |
|---|---|
| Typical Space | O(1) |
Common Variations
- Basic Binary Search
- Binary Search with constraints
- Optimized Binary Search
Practice Problems
See all Binary Search problems →Practice Binary Search
Learn to recognize patterns instantly with interactive MCQs.
Download LeetEye Free