MediumArrayBinary SearchUpdated Sep 2026

875. Koko Eating Bananas

Asked at Amazon, Atlassian, Netflix, NVIDIA

Difficulty

Medium

Time

O(n * log(max(piles)))

Space

O(1)

Pattern

Array

Companies

4

Problem

Koko has n piles of bananas. The i-th pile holds piles[i] bananas. The guards are away and will return in h hours.

Koko picks an eating speed k, measured in bananas per hour. In each hour she chooses one pile and eats up to k bananas from it. If the pile has fewer than k bananas left, she finishes that pile and eats nothing more during that hour. So eating one pile takes ceil(pile / k) hours.

Return the smallest integer k that lets Koko finish every pile within h hours. She wants to eat as slowly as possible, but she must be done before the guards come back.

Key rule: if a speed k works, every larger speed also works. That monotonic property is what makes binary search possible.

Pick a speed, count the hours

piles367110123The biggest pile is 11, so any speed above 11 is neveruseful.hours at k = 40123pilehours367111223At speed 4 the hours are 1, 2, 2, 3 — a total of 8,which exactly fits h = 8.
Koko eats one pile per hour at speed k, so a pile of p bananas costs ceil(p / k) hours. We need the smallest k whose total is at most h.

Constraints

  • ●1 <= piles.length <= 10^4
  • ●piles.length <= h <= 10^9
  • ●1 <= piles[i] <= 10^9

Examples

Example 1
Inputpiles = [3,6,7,11], h = 8
Output4
WhyAt speed 4 the piles take ceil(3/4)+ceil(6/4)+ceil(7/4)+ceil(11/4) = 1+2+2+3 = 8 hours, which fits. At speed 3 it takes 1+2+3+4 = 10 hours, too many. So 4 is the slowest speed that still works.
Example 2
Inputpiles = [30,11,23,4,20], h = 5
Output30
WhyThere are 5 piles and only 5 hours, so Koko must finish one whole pile each hour. That is only possible if she can clear the biggest pile (30 bananas) in a single hour, so the speed must be at least 30.
Example 3
Inputpiles = [30,11,23,4,20], h = 6
Output23
WhyWith one extra hour she can split the largest pile across two hours. At speed 23 the hours are 2+1+1+1+1 = 6, which fits. At speed 22 the first pile needs ceil(30/22) = 2 hours and the third needs ceil(23/22) = 2 hours, giving 7 hours total, too many.

Asked At

4 companies

How to Think About It

1

Brute force: try every speed from 1 to max(piles). For each speed, calculate the total hours needed by summing ceil(pile / k) for each pile. Return the first speed where total hours <= h. This is O(max(piles) * n) which is too slow for large inputs.

2

Key insight: the answer space is monotonic. If speed k works, then any speed > k also works. If speed k does not work, any speed < k also does not work. This is a classic binary search on answer scenario.

3

Binary search bounds: lo = 1 (minimum possible speed), hi = max(piles) (eat the largest pile in one hour). For each mid speed, calculate hours needed. If hours <= h, the answer is mid or lower (search left). If hours > h, we need a higher speed (search right).

4

Time complexity: O(n * log(max(piles))) where n is the number of piles. Each binary search step takes O(n) to compute total hours, and we do O(log(max(piles))) steps. Space: O(1).

5

Edge cases: single pile, pile size of 1, h equals n (must eat exactly 1 per pile per hour), h very large (answer is 1).

Optimal Approach

Binary search on the answer (eating speed k). Set lo = 1, hi = max(piles). While lo < hi: compute mid = (lo + hi) // 2. Calculate total hours: for each pile, add ceil(pile / mid). If total hours <= h, set hi = mid (try slower). Else set lo = mid + 1 (need faster). Return lo.

Walkthrough: piles = [3, 6, 7, 11], h = 8.

  • lo=1, hi=11, mid=6. Hours = 1+1+2+2 = 6 <= 8. hi=6.
  • lo=1, hi=6, mid=3. Hours = 1+2+3+4 = 10 > 8. lo=4.
  • lo=4, hi=6, mid=5. Hours = 1+1+2+3 = 7 <= 8. hi=5.
  • lo=4, hi=5, mid=4. Hours = 1+2+2+3 = 8 <= 8. hi=4.
  • lo=4, hi=4. Return 4.

Time: O(n * log(max(piles))). Space: O(1).

Binary search on the speed

lo = 1, hi = max(piles)mid = (lo + hi) // 2hours = sum(ceil(p / mid))hours <= h ? hi = mid : lo = mid + 1Repeat until lo == hi; that meeting point is theanswer.search on piles=[3,6,7,11], h=8lohimidhoursmovestep 1step 2step 3step 411166hi = 616310lo = 44657hi = 54548hi = 4lo and hi close in on 4; the last step checks mid = 4,gets exactly 8 hours, and sets hi = 4.hours(k) = sum(ceil(p / k))hours is non-increasing in kanswer = smallest k with hours(k) <= h
The hours needed only go down as the speed goes up, so we can binary search for the smallest speed that fits.
ComplexityTime O(n * log(max(piles)))Space O(1)Each binary search step scans all n piles to count hours, and the search range shrinks by half about log(max(piles)) times.

What Trips People Up in Real Interviews

1

Using (p + mid - 1) / mid for ceiling division but forgetting it for the Python math.ceil version. In C++/Java, (p + mid - 1) / mid avoids floating point. In Python, math.ceil(p / mid) works but is slower than integer math.

2

Setting hi to sum(piles) instead of max(piles). The maximum speed Koko could ever need is eating the largest pile in one hour. Using sum(piles) wastes binary search iterations.

3

Off-by-one in the binary search: returning lo - 1 or using < instead of <= when checking hours. The loop invariant is lo <= answer <= hi. When lo == hi, that is the answer.

4

Not using long for the hours sum in C++/Java. If you have many piles of large bananas, the sum can overflow int. Always use long for the running total.

5

Trying a greedy approach (sort piles, eat from largest first) instead of binary search. Greedy doesn't work here because the optimal speed depends on all piles simultaneously, not just the largest one.

Solution Code

O(n * log(max(piles))) time
import math

def minEatingSpeed(piles, h):
    lo, hi = 1, max(piles)
    while lo < hi:
        mid = (lo + hi) // 2
        hours = sum(math.ceil(p / mid) for p in piles)
        if hours <= h:
            hi = mid
        else:
            lo = mid + 1
    return lo

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Koko Eating Bananas problem?

Koko loves to eat bananas. There are n piles of bananas, the i-th pile has `piles[i]` bananas. The guards have gone and will come back in h hours. Koko can decide her eating speed k (bananas per hour). Each hour, she chooses a pile and eats k bananas from it. If the pile has fewer than k bananas, she eats all of them and does not eat any more that hour. Find the minimum integer k such that she can eat all the bananas within h hours.

How do you solve Koko Eating Bananas?

The optimal approach runs in O(n * log(max(piles))) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Koko Eating Bananas?

Koko Eating Bananas is asked at Amazon, Atlassian, Netflix, NVIDIA. It is a medium difficulty problem.

What are common mistakes on Koko Eating Bananas?
  • Using `(p + mid - 1) / mid` for ceiling division but forgetting it for the Python `math.ceil` version. In C++/Java, `(p + mid - 1) / mid` avoids floating point. In Python, `math.ceil(p / mid)` works but is slower than integer math.
  • Setting `hi` to `sum(piles)` instead of `max(piles)`. The maximum speed Koko could ever need is eating the largest pile in one hour. Using `sum(piles)` wastes binary search iterations.
  • Off-by-one in the binary search: returning `lo - 1` or using `<` instead of `<=` when checking hours. The loop invariant is `lo <= answer <= hi`. When `lo == hi`, that is the answer.
  • Not using `long` for the hours sum in C++/Java. If you have many piles of large bananas, the sum can overflow `int`. Always use `long` for the running total.
  • Trying a greedy approach (sort piles, eat from largest first) instead of binary search. Greedy doesn't work here because the optimal speed depends on all piles simultaneously, not just the largest one.