PythonStudio
Interview Problems

Koko Eating Bananas

medium · binary search on answer

Koko loves bananas. There are piles of bananas, where piles[i] is the number of bananas in pile i, and she has `h` hours before the guards return. Each hour she chooses one pile and eats up to `k` bananas from it (if the pile has fewer than k, she eats the whole pile and the hour still counts). Return the smallest integer speed `k` (bananas per hour) that lets her finish all the piles within `h` hours. Input: `piles` (a list of integers) and `h` (an integer). Output: the smallest integer eating speed k. Example: piles = [3, 6, 7, 11], h = 8 -> 4.

Starter code

def min_eating_speed(piles: list[int], h: int) -> int:
    """Return the smallest eating speed (bananas/hour) that finishes in h hours."""
    pass

Enable JavaScript to edit this code and run the hidden tests in your browser. Each test has a five-second execution limit. Your drafts and solved progress stay in this browser.

Return to the Python compiler