By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This problem appears in 1 out of every 3 FAANG onsite interviews—mastering it proves you can optimize under constraints and apply binary search beyond sorted arrays, a skill that separates L4 from L5 engineers."
Before tackling this problem, ensure you understand: 1. Binary Search – How to search in a sorted array in O(log n) time. 2. Time Complexity Analysis – How to compute the runtime of a loop or function. 3. Edge Case Handling – How to handle minimum/maximum bounds and integer overflows.
k
k+1
f(k) ≥ target
f(k)
k=3
k=4
f(k) ≥ h
1e9
mid = left + (right - left) // 2
(left + right) // 2
ceil(pile / k)
Follow these steps every time you see a "minimum/maximum under constraints" problem:
left
right
Example: For Koko, left = 1 (slowest speed), right = max(piles) (fastest speed).
left = 1
right = max(piles)
Define the Condition Function
True
Example: f(k) = True if Koko can eat all bananas in ≤ h hours at speed k.
f(k) = True
≤ h
Check Monotonicity
f(k+1)
If not, binary search won’t work—rethink the approach.
Binary Search Setup
While left < right:
left < right
f(mid)
right = mid
left = mid + 1
Return the Answer
Verify with f(left) (optional, but good for debugging).
f(left)
Edge Cases & Optimization
h < len(piles)
-1
h == len(piles)
max(piles)
Problem: Koko wants to eat all bananas in piles within h hours. She can eat at most k bananas per hour. Find the minimum k such that she finishes in ≤ h hours.
piles
h
Solution (Step-by-Step): 1. Search Space: - left = 1 (slowest speed). - right = max(piles) (fastest speed, eats one pile in 1 hour).
Condition Function (canEat): python def canEat(piles, k, h): hours = 0 for pile in piles: hours += (pile + k - 1) // k # Equivalent to math.ceil(pile / k) return hours <= h
canEat
python def canEat(piles, k, h): hours = 0 for pile in piles: hours += (pile + k - 1) // k # Equivalent to math.ceil(pile / k) return hours <= h
Binary Search: python def minEatingSpeed(piles, h): left, right = 1, max(piles) while left < right: mid = left + (right - left) // 2 if canEat(piles, mid, h): right = mid else: left = mid + 1 return left
python def minEatingSpeed(piles, h): left, right = 1, max(piles) while left < right: mid = left + (right - left) // 2 if canEat(piles, mid, h): right = mid else: left = mid + 1 return left
Time Complexity:
O(n log m)
n = len(piles)
m = max(piles)
canEat is O(n), and binary search runs O(log m) times.
O(n)
O(log m)
Why This Works:
canEat(k)
Problem: You have n buses, each with a time[i] to complete one trip. Find the minimum time to complete at least totalTrips trips.
n
time[i]
totalTrips
Solution: 1. Search Space: - left = 1 (minimum possible time). - right = min(time) totalTrips (worst case: slowest bus does all trips).
right = min(time) totalTrips
Condition Function (canComplete): python def canComplete(time, totalTrips, T): trips = 0 for t in time: trips += T // t return trips >= totalTrips
canComplete
python def canComplete(time, totalTrips, T): trips = 0 for t in time: trips += T // t return trips >= totalTrips
Binary Search: python def minimumTime(time, totalTrips): left, right = 1, min(time) totalTrips while left < right: mid = left + (right - left) // 2 if canComplete(time, totalTrips, mid): right = mid else: left = mid + 1 return left
python def minimumTime(time, totalTrips): left, right = 1, min(time) totalTrips while left < right: mid = left + (right - left) // 2 if canComplete(time, totalTrips, mid): right = mid else: left = mid + 1 return left
T
Problem: You have bloomDay array where bloomDay[i] is the day flower i blooms. You need m bouquets, each with k adjacent flowers. Find the minimum number of days to make m bouquets.
bloomDay
bloomDay[i]
i
m
Solution: 1. Search Space: - left = 1 (minimum day). - right = max(bloomDay) (maximum day).
right = max(bloomDay)
Condition Function (canMakeBouquets): python def canMakeBouquets(bloomDay, m, k, day): bouquets = 0 flowers = 0 for bloom in bloomDay: if bloom <= day: flowers += 1 if flowers == k: bouquets += 1 flowers = 0 else: flowers = 0 return bouquets >= m
canMakeBouquets
python def canMakeBouquets(bloomDay, m, k, day): bouquets = 0 flowers = 0 for bloom in bloomDay: if bloom <= day: flowers += 1 if flowers == k: bouquets += 1 flowers = 0 else: flowers = 0 return bouquets >= m
Binary Search: python def minDays(bloomDay, m, k): if m k > len(bloomDay): return -1 left, right = 1, max(bloomDay) while left < right: mid = left + (right - left) // 2 if canMakeBouquets(bloomDay, m, k, mid): right = mid else: left = mid + 1 return left
python def minDays(bloomDay, m, k): if m k > len(bloomDay): return -1 left, right = 1, max(bloomDay) while left < right: mid = left + (right - left) // 2 if canMakeBouquets(bloomDay, m, k, mid): right = mid else: left = mid + 1 return left
day
right = len(piles)
max(bloomDay)
while left <= right
left = mid
while left < right
math.ceil(pile / k)
(pile + k - 1) // k
left + (right - left) // 2
"Here’s the 30-second version before your interview: 1. Identify the search space: left is the smallest possible answer, right is the largest. 2. Write f(k): A helper that checks if k works. 3. Check monotonicity: If k works, then k+1 must work. 4. Binary search: While left < right, adjust left or right based on f(mid). 5. Return left: It’s the smallest valid k.
This pattern appears in minimum/maximum under constraints problems—like Koko’s bananas, bus trips, or bouquets. If you see ‘minimum time’ or ‘maximum speed,’ think binary search on answer.
Now go crush that interview!"
O(1)
You’re now ready to solve any "binary search on answer" problem in a FAANG interview! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.