Skip to main content
← Problem catalogue Understand the pattern. Try it yourself. Review the solution. Practice this pattern →
Medium Binary Search Medium frequency Solution Available Practice Mode

Koko Eating Bananas LeetCode Solution

Tags: #Array #Binary Search

Approach Summary

Binary search on the answer (eating speed). Feasibility check: can Koko finish all bananas at speed k within h hours?

Practice Mode

Solve it yourself before revealing the code

DSA interviews test reasoning from brute force to optimal. Think through the constraints first.

Algorithmic Pattern

Master the Binary Search Pattern

View Pattern Guide →

Study the core invariant, step-by-step decision framework, and common interview traps for the Binary Search pattern.

Framework: Binary Search • Interview Prep: Tested at FAANG • Explore all Binary Search problems & invariants →

How to Recognize This Pattern

  • Minimum rate/speed that satisfies a constraint

Complexity Analysis

Time Complexity

O(n log m)

Space Complexity

O(1)

Tags

Array Binary Search

Frequently Asked Questions

What is the optimal time and space complexity for Koko Eating Bananas?
The optimal solution for Koko Eating Bananas achieves O(n log m) runtime complexity and requires O(1) auxiliary space. Binary search over a range of size max(piles) with an O(n) check per step — O(n log(max(piles))). Only a few scalars — O(1) space.
How do I identify that Koko Eating Bananas requires the Binary Search pattern?
In technical interviews, look for key problem signals: Minimum rate/speed that satisfies a constraint. Recognizing these invariants allows you to bypass naive brute-force exhaustive search in favor of Binary Search strategy.
What critical edge cases must be handled in Koko Eating Bananas?
Essential edge cases to verify include: h equal to len(piles) — Koko must eat one pile per hour; speed equals the largest pile; A single pile — speed is ceil(pile / h); h larger than needed — speed 1 may already suffice; Piles larger than h — impossible inputs are excluded by constraints. Always walk through these scenarios with your interviewer before coding.
Which tech companies frequently ask Koko Eating Bananas?
Koko Eating Bananas is a Medium-difficulty question tagged with medium recurrence. It is actively asked in technical screening and onsite interview loops at Facebook, Amazon.
Buy me a coffee
Daily Practice
0/0