DSA Tracker

Medium

Koko Eating Bananas

A medium Binary Search problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Binary Search
Sheets
1
Core for
7 roles
Platform
LeetCode

The problem

Koko loves to eat bananas. There are n piles of bananas, and the guards will come back in h hours. Koko can decide her bananas-per-hour eating speed k. Each hour she picks a pile and eats k bananas from it (or all remaining if less than k). Return the minimum integer k such that she can eat all bananas within h hours.

Example 1

Input
piles = [3,6,7,11], h = 8
Output
4
Why
At speed 4, Koko finishes all piles within 8 hours.

Example 2

Input
piles = [30,11,23,4,20], h = 5
Output
30
Why
At speed 30, she finishes each pile in one hour, taking 5 hours total.

Constraints

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

How to think about it

Updated 2026-09-09

Total eating time is a monotonically decreasing function of speed: faster speeds never increase the hours required. Because the outcome transitions cleanly from impossible to possible at a single speed threshold, you can binary search directly on the speed value.

Approaches, worst first

  1. Sequential speed increment

    time O(max(piles) * n) · space O(1)

    Start speed k at 1 and compute total hours until the sum drops below or equals h. Correct due to monotonicity, but scanning speeds one by one is far too slow when piles contain up to 10^9 bananas.

  2. Binary search on eating rateWrite this one

    time O(n log(max(piles))) · space O(1)

    Binary search over speeds between 1 and the maximum pile size. For each midpoint speed, accumulate ceiling division hours across all piles to check feasibility within h hours.

Where people lose marks · 3
  • Ceil division computed with standard floating-point arithmetic introduces rounding precision loss on numbers near 10^9; use integer arithmetic (pile + speed - 1) / speed.
  • Summing hours across piles into a 32-bit signed integer can exceed 2^31 - 1 when speeds are very small; use 64-bit integer accumulators.
  • Setting the search upper bound to a fixed small constant instead of max(piles) fails when h equals the number of piles.

The theory behind it

Binary Search — the ground this problem stands on. All Binary Search problems

What Binary Search is

Binary search is the guessing strategy used when searching a thick telephone directory or guessing a secret number between one and a hundred. Rather than inspecting names one by one from the first page, the search opens straight to the middle page. If the target precedes that middle entry, the entire back half is discarded; if it follows, the front half is eliminated. Repeating this halved split finds the item with remarkable speed.

When to reach for it

Reach for binary search when queries target sorted collections, rotated sorted arrays, or monotonic answer spaces. Strong hints include logarithmic time constraints like O(log n) or search ranges exceeding one billion where stepping one value at a time times out. It also applies when validating whether a guessed solution is possible via a monotonic boolean check, known as binary search on answer.

How the pattern works

Define the search territory with two inclusive pointers, low and high. Compute the midpoint using low plus half the difference to high, avoiding integer overflow. Formulate an exact boolean condition that divides the range into true and false halves. Decide whether the boundary condition includes the midpoint or shifts strictly past it. The loop invariant states that the sought target, if it exists, remains trapped inside the interval throughout every iteration.

What each operation costs

OperationTime
find target in sorted arrayO(log n)
find boundary in monotonic rangeO(log n)
What usually goes wrong with Binary Search
  • Triggering integer overflow by computing middle using low plus high divided by two instead of low plus half of high minus low in fixed-width numeric types.
  • Creating an infinite loop when the interval shrinks to two elements by setting low equal to mid when mid was rounded down.
  • Mismatched loop condition and bounds updates, such as pairing low less than or equal to high with non-advancing pointer assignments that never terminate.

Which roles need this problem

Binary Search is a core topic for these 7 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 17 more roles, including Full-Stack Developer, Data Engineer, Android Developer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Binary Search problems

Problem set and role mapping as of .