Coding interview problems โบ Binary Search
๐ Binary Search Medium
Koko Eating Bananas (Binary Search on the Answer)
Koko Eating Bananas explained: binary search on the answer to find the minimum speed that finishes in h hours, tested Python, mistakes and a live visual.
Also searched as: binary search on answer ยท minimum eating speed ยท binary search on answer space problems
Try it live Free
Binary Search
In the binary search lab, watch how each comparison throws away half the range; here the "array" is the list of candidate speeds and each look asks whether a speed is fast enough.
The problem
There are several piles of bananas, piles[i] in pile i. Each hour Koko picks one pile and eats up to k bananas from it (if the pile has fewer, she finishes it and waits for the next hour). Find the smallest integer speed k that lets her finish every pile within h hours. Assume h is at least the number of piles.
At speed 4 the piles take 1 + 2 + 2 + 3 = 8 hours; speed 3 needs 10.
The key insight
You are not searching an array; you are searching the range of possible answers, speeds from 1 to max(piles). The check "can she finish at speed k?" is monotonic: if speed k works, every faster speed also works. So binary search for the first speed where the check turns true.
Step-by-step approach
- Define
hours(k)= sum ofceil(p / k)over all piles, computed as(p + k - 1) // k. - Set
lo = 1andhi = max(piles); speedmax(piles)always finishes inlen(piles)hours. - While
lo < hi, takemid = (lo + hi) // 2. - If
hours(mid) <= h, mid works, so the answer is mid or smaller:hi = mid. - Otherwise mid is too slow:
lo = mid + 1. - Return
lo.
Python solution
def min_eating_speed(piles, h):
def hours(k):
return sum((p + k - 1) // k for p in piles) # ceil(p / k)
lo, hi = 1, max(piles)
while lo < hi:
mid = (lo + hi) // 2
if hours(mid) <= h: # fast enough: try slower
hi = mid
else: # too slow
lo = mid + 1
return loComplexity
There are O(log M) binary search steps, where M is the largest pile, and each step sums over all n piles.
Common mistakes
- Using
p // kinstead of a ceiling, which undercounts the hours for any pile that is not an exact multiple of k. - Starting the search at 0, which leads to a division by zero.
- Searching for any speed that finishes in exactly
hhours; the answer often finishes early, so you want the first speed where the check is true.
Frequently asked questions
What is the time and space complexity?
O(n log M) time, where n is the number of piles and M is the largest pile, and O(1) extra space.
How do I recognise a binary-search-on-the-answer problem?
Look for wording like minimum speed, minimum capacity or smallest maximum, plus a yes or no check that flips only once as the answer grows. Ship Packages Within D Days and Split Array Largest Sum use the same template.
Why is max(piles) a safe upper bound?
At that speed every pile is eaten in one hour, so the total is len(piles) hours, which fits because h is at least the number of piles.
Can I compute the ceiling with math.ceil(p / k)?
It works for typical inputs, but floating-point division can misround huge values. The integer form (p + k - 1) // k is exact and avoids that risk.
Preparing for placements?
Take the 2-minute readiness check to find your weak patterns, then follow the placement path from DSA core to system design.
All coding interview problems More Binary Search Binary Search lab Placement prep guides
Problem statements on this site are written in our own words. Problem names are the common names used across interview-prep communities; BitBootcamp is an independent learning platform and is not affiliated with LeetCode or any company that asks these questions.