Koko Eating Bananas: 6 Best Tips to Solve It Easily
Koko Eating Bananas is Day 25 of the Tekraze DSA series, and it hides a sneaky upgrade to binary search. In the binary search chapter we first searched a sorted array in Day 23, Binary Search and then a sorted grid in Day 24, Search a 2D Matrix. Today we stop searching the input and start searching the answer itself. This one idea unlocks a whole family of hard looking interview questions.
Also Read:Search a 2D Matrix: 6 Best Tips to Solve It Easily and our DSA series hub for every day in order.
What is the Koko Eating Bananas problem?
Koko has n piles of bananas, where piles[i] is the size of pile i. The guards return in h hours. Each hour Koko picks one pile and eats k bananas from it. If the pile has fewer than k, she finishes it and waits for the next hour.
Return the smallest whole number k that lets her eat everything within h hours. You can assume h is at least the number of piles.
Input: piles = [3, 6, 7, 11], h = 8 Output: 4 Input: piles = [30, 11, 23, 4, 20], h = 5 Output: 30 Input: piles = [30, 11, 23, 4, 20], h = 6 Output: 23
In the first example, speed 4 is the slowest speed that still fits inside 8 hours. Anything slower runs out of time.
How should you think about it in plain words?
Think of Koko Eating Bananas as a yes or no test you can run on any speed. Pick any speed and you can quickly check if it works. A pile of size p takes ceil(p / k) hours, so just add those up and compare with h.
Now notice something nice. If speed 4 works, then 5, 6 and every faster speed also work. If speed 3 fails, every slower speed fails too. The speeds line up as fail, fail, fail, pass, pass, pass. Finding the first pass in a sorted yes or no line is exactly what binary search does best.
Why is that always true? Raising k can never make a pile take longer, so the total hours only stay the same or go down. That one sentence is the proof for Koko Eating Bananas, and it is the part interviewers want to hear. The chart below shows it for the first example, where speeds 1, 2 and 3 need 27, 15 and 10 hours and everything from speed 4 onward fits in 8.

What does the brute force approach look like?
The brute force answer to Koko Eating Bananas is easy to write. Try every speed from 1 upward and return the first one that finishes in time.
def min_speed_brute(piles, h):
k = 1
while True:
if sum((p + k - 1) // k for p in piles) <= h:
return k
k += 1
In the worst case this tries every speed up to the biggest pile, so it costs O(n * max(piles)) time and O(1) space. With piles as large as a billion that never finishes in an interview. Brute force may try a billion speeds, while binary search needs about 30 checks. That gap is why Koko Eating Bananas is a binary search question.

How do we get the optimal solution?
To solve Koko Eating Bananas fast, fix the search range first. The answer always sits between 1 and max(piles), because eating faster than the biggest pile never saves an hour. Set left = 1 and right = max(piles). Take the middle speed and check it.
If Koko can finish, that speed might be the answer, so keep it with right = mid. If she cannot, every slower speed fails too, so jump with left = mid + 1. When the two meet, you have the smallest speed that works.
How does Koko Eating Bananas work on the first example?
Take piles [3, 6, 7, 11] with h = 8. The range starts at 1 to 11.
- Speed 6 takes 1 + 1 + 2 + 2 = 6 hours, which passes, so the range shrinks to 1 to 6.
- Speed 3 takes 1 + 2 + 3 + 4 = 10 hours, which fails, so we move to 4 to 6.
- Speed 5 takes 1 + 2 + 2 + 3 = 8 hours, which passes, giving 4 to 5.
- Speed 4 takes 1 + 2 + 2 + 3 = 8 hours, which passes, so the answer is 4.

What is the time and space complexity?
For Koko Eating Bananas, time is O(n log M), where M is the biggest pile, because each of the log M checks walks all n piles. Space is O(1). If the log part still feels fuzzy, our Big O helper post on time and space complexity explains why halving a range is so fast.
Koko Eating Bananas solutions in Java, Python and Rust
Every Koko Eating Bananas solution below follows the same plan: a small checker function, then a left less than right loop. Pick the language you practice in, or read all three to compare.
Java solution for Koko Eating Bananas
public class Solution {
public static int minEatingSpeed(int[] piles, int h) {
int left = 1, right = 1;
for (int p : piles) right = Math.max(right, p);
while (left < right) {
int mid = left + (right - left) / 2;
if (canFinish(piles, mid, h)) right = mid;
else left = mid + 1;
}
return left;
}
static boolean canFinish(int[] piles, int k, int h) {
long hours = 0;
for (int p : piles) hours += (p + (long) k - 1) / k;
return hours <= h;
}
public static void main(String[] args) {
check(minEatingSpeed(new int[]{3, 6, 7, 11}, 8), 4);
check(minEatingSpeed(new int[]{30, 11, 23, 4, 20}, 5), 30);
check(minEatingSpeed(new int[]{30, 11, 23, 4, 20}, 6), 23);
check(minEatingSpeed(new int[]{1}, 1), 1);
check(minEatingSpeed(new int[]{1000000000}, 2), 500000000);
check(minEatingSpeed(new int[]{312884470}, 968709470), 1);
check(minEatingSpeed(new int[]{805306368, 805306368, 805306368}, 1000000000), 3);
System.out.println("All Java tests passed");
}
static void check(int got, int want) {
if (got != want) throw new AssertionError("got " + got + " want " + want);
}
}
The hour total is a long because a slow speed on huge piles can add up past the int limit.
Python solution for Koko Eating Bananas
def min_eating_speed(piles, h):
def can_finish(k):
return sum((p + k - 1) // k for p in piles) <= h
left, right = 1, max(piles)
while left < right:
mid = (left + right) // 2
if can_finish(mid):
right = mid
else:
left = mid + 1
return left
if __name__ == "__main__":
assert min_eating_speed([3, 6, 7, 11], 8) == 4
assert min_eating_speed([30, 11, 23, 4, 20], 5) == 30
assert min_eating_speed([30, 11, 23, 4, 20], 6) == 23
assert min_eating_speed([1], 1) == 1
assert min_eating_speed([1000000000], 2) == 500000000
assert min_eating_speed([312884470], 968709470) == 1
assert min_eating_speed([805306368] * 3, 1000000000) == 3
print("All Python tests passed")
Rust solution for Koko Eating Bananas
The Rust version does the hour math in i64 for the same overflow reason, and uses the same left less than right loop as the other two.
fn can_finish(piles: &[i32], k: i64, h: i64) -> bool {
let hours: i64 = piles.iter().map(|&p| (p as i64 + k - 1) / k).sum();
hours <= h
}
fn min_eating_speed(piles: &[i32], h: i32) -> i32 {
let (mut left, mut right) = (1i64, *piles.iter().max().unwrap() as i64);
while left < right {
let mid = left + (right - left) / 2;
if can_finish(piles, mid, h as i64) {
right = mid;
} else {
left = mid + 1;
}
}
left as i32
}
fn main() {
assert_eq!(min_eating_speed(&[3, 6, 7, 11], 8), 4);
assert_eq!(min_eating_speed(&[30, 11, 23, 4, 20], 5), 30);
assert_eq!(min_eating_speed(&[30, 11, 23, 4, 20], 6), 23);
assert_eq!(min_eating_speed(&[1], 1), 1);
assert_eq!(min_eating_speed(&[1000000000], 2), 500000000);
assert_eq!(min_eating_speed(&[312884470], 968709470), 1);
assert_eq!(min_eating_speed(&[805306368, 805306368, 805306368], 1000000000), 3);
println!("All Rust tests passed");
}
All three Koko Eating Bananas programs ran the three sample cases, a single pile, a one billion banana pile, a case where h is huge so speed 1 works, and a case where the hour total overflows a 32 bit int. Every check passed in Java, Python and Rust.
What are the tips and tricks for an easy solution?
Here are six habits that make Koko Eating Bananas, and problems like it, feel easy.
- Spot the pattern. When a question asks for the minimum or maximum value that still satisfies a rule, and the rule flips only once as the value grows, think binary search on the answer, just like today’s problem.
- Write the checker first. A small can_finish(k) function keeps the binary search loop clean and easy to test on its own.
- Ceil without floats. Use (p + k – 1) / k instead of floating point division to avoid rounding bugs.
- Pick tight bounds. Low is 1 and high is the biggest pile. A smaller range means fewer checks.
- Test the edge cases. One pile, h equal to the number of piles (answer is the max pile), a huge h (answer is 1) and very large pile values that overflow int sums.
- Explain the monotonic idea. Say out loud why the answer to Koko Eating Bananas is monotonic. Interviewers care more about that reasoning than the loop itself.
What are the common mistakes people make?
These five slips cost people the most points on Koko Eating Bananas.
- Starting left at 0, which leads to dividing by zero in the checker.
- Using right = mid – 1 after a passing speed, which can skip the true answer.
- Summing hours in a 32 bit int, which overflows on big inputs in Java and Rust.
- Using floating point ceil, which can round wrong on large values.
- Searching over the piles array instead of over the range of speeds.
Where is this pattern used in the real world?
The idea behind Koko Eating Bananas shows up whenever you tune one number against a limit. Capacity planning can pick the smallest server count that keeps response time under a target. Batch jobs can choose the lowest throughput that still meets a deadline.
Streaming players can look for the highest bitrate that fits the bandwidth, and shipping tools can find the smallest truck capacity that delivers everything in D days, which is the cousin problem Capacity To Ship Packages. The picture below puts the two kinds of binary search side by side.

What should you remember about Koko Eating Bananas?
Koko Eating Bananas teaches one habit: when the rule flips once, search the answer. Write the checker, set tight bounds, and let binary search do the rest. The same habit solves every cousin problem in the FAQ below.
Frequently asked questions
What is the Koko Eating Bananas problem?
Koko has piles of bananas and h hours. She eats up to k bananas from one pile each hour. You must find the smallest k that lets her finish every pile within h hours.
Why does binary search work for Koko Eating Bananas?
Because the answer is monotonic. If a speed lets Koko finish in time, every faster speed also works, so the speeds form a fail then pass line and binary search finds the first pass.
What is the time complexity of Koko Eating Bananas?
The optimal solution runs in O(n log M) time, where n is the number of piles and M is the largest pile, and it uses O(1) extra space.
What is binary search on the answer?
It is a technique where you binary search over the range of possible answers instead of over an array, using a checker function to decide which half to keep. Koko Eating Bananas is the classic first example.
Which problems are similar to Koko Eating Bananas?
Capacity To Ship Packages Within D Days, Split Array Largest Sum, Minimum Number of Days to Make Bouquets and Magnetic Force Between Two Balls all use the same binary search on the answer pattern.





