By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"Binary search isn’t just about finding a number—it’s the fastest way to prove you can write clean, efficient code under pressure. FAANG interviewers use it to test your ability to handle edge cases, optimize brute-force solutions, and think in logarithmic time. Master this, and you’ll solve 1 in 4 medium/hard problems in under 30 minutes."
Before diving into binary search, ensure you understand: 1. Sorted Arrays – Binary search only works on sorted data (or data that can be partitioned in a sorted way). 2. Time Complexity – O(log n) vs. O(n) and why it matters in interviews. 3. Loop Invariants – How to maintain correctness in while loops (e.g., left <= right vs. left < right).
left <= right
left < right
nums = [1,3,5,7], target = 5
2
nums = [1,2,2,2,3], target = 2
1
3
[4,5,6,1,2,3]
nums = [4,5,6,1,2,3], target = 1
nums[i] > nums[i+1]
-∞
nums = [1,2,3,1]
nums[i] >= target
nums[i] > target
nums = [1,2,4,4,5], target = 3
4
Run this every time you see a binary search problem:
If not, ask: "Can I sort it first?" (If yes, sort and proceed. If no, binary search is likely not the answer.)
Define left and right pointers.
left
right
left = 0
right = len(nums) - 1
For subarrays, adjust left and right accordingly.
Choose the loop condition:
while left <= right
mid
target
while left < right: Use when you’re narrowing the search space (e.g., first/last occurrence).
while left < right
Calculate mid safely to avoid overflow.
mid = left + (right - left) // 2 (never left + right // 2).
mid = left + (right - left) // 2
left + right // 2
Decide how to move left and right:
nums[mid] < target
left = mid + 1
nums[mid] > target
right = mid - 1
If nums[mid] == target → depends on the problem (e.g., return mid, or search left/right for first/last occurrence).
nums[mid] == target
Handle edge cases:
-1
None
All elements equal → ensure the loop terminates.
Test with small examples:
[1,3,5]
[1,1,1]
0
Problem: Given a sorted array nums and a target target, return the index of target if it exists, else -1.
nums
Framework Application: 1. Input is sorted → proceed. 2. left = 0, right = len(nums) - 1. 3. Loop condition: while left <= right. 4. mid = left + (right - left) // 2. 5. If nums[mid] == target → return mid. - If nums[mid] < target → left = mid + 1. - If nums[mid] > target → right = mid - 1. 6. If loop ends → return -1.
Code (Python):
def search(nums, target): 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
Time Complexity: O(log n) Space Complexity: O(1)
Why this works: - The loop halves the search space each iteration, ensuring logarithmic time. - The left <= right condition ensures we check every possible element.
Problem: Given a sorted array nums with duplicates, return the first occurrence of target. If not found, return -1.
Framework Application: 1. Input is sorted → proceed. 2. left = 0, right = len(nums) - 1. 3. Loop condition: while left <= right. 4. mid = left + (right - left) // 2. 5. If nums[mid] == target → record mid and search left for earlier occurrences (right = mid - 1). - If nums[mid] < target → left = mid + 1. - If nums[mid] > target → right = mid - 1. 6. Return the recorded index (or -1 if not found).
def first_occurrence(nums, target): left, right = 0, len(nums) - 1 result = -1 while left <= right: mid = left + (right - left) // 2 if nums[mid] == target: result = mid right = mid - 1 # Search left for earlier occurrence elif nums[mid] < target: left = mid + 1 else: right = mid - 1 return result
Why this works: - When we find target, we don’t return immediately—we keep searching left to find the first occurrence. - The result variable tracks the earliest valid index.
result
Problem: Given a sorted array nums with duplicates, return the last occurrence of target. If not found, return -1.
Framework Application: 1. Input is sorted → proceed. 2. left = 0, right = len(nums) - 1. 3. Loop condition: while left <= right. 4. mid = left + (right - left) // 2. 5. If nums[mid] == target → record mid and search right for later occurrences (left = mid + 1). - If nums[mid] < target → left = mid + 1. - If nums[mid] > target → right = mid - 1. 6. Return the recorded index (or -1 if not found).
def last_occurrence(nums, target): left, right = 0, len(nums) - 1 result = -1 while left <= right: mid = left + (right - left) // 2 if nums[mid] == target: result = mid left = mid + 1 # Search right for later occurrence elif nums[mid] < target: left = mid + 1 else: right = mid - 1 return result
Why this works: - Similar to first occurrence, but we search right after finding target to find the last occurrence. - The result variable tracks the latest valid index.
mid = (left + right) // 2
right = len(nums)
len(nums) - 1
if not nums: return -1
len(nums)
nums[right] >= target
"Alright, let’s lock this in. Binary search is all about halving the search space in logarithmic time. Here’s your 30-second checklist:
left + (right - left) // 2
Now go crush that interview. You’ve got this!
Good luck! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.