DSA interview question
Koko has n piles of bananas and h hours before the guards return. Each hour she picks one pile and eats up to k bananas from it; if the pile has fewer than k, she finishes it and waits for the next hour. She wants to eat slowly, so she needs the minimum speed k that still finishes every pile within h hours. Trying every speed from 1 upwards is far too slow when piles hold 10⁹ bananas. But feasibility is monotonic: any speed faster than a working speed also works. So binary search on k.
Example: piles = [3, 6, 7, 11], h = 8 → 4.
int minEatingSpeed(const vector<int>& piles, long long h); // h >= number of piles
Input (piles, h) | Output | Explanation |
|---|---|---|
([3, 6, 7, 11], 8) | 4 | 1 + 2 + 2 + 3 = 8 hours |
([30, 11, 23, 4, 20], 5) | 30 | One hour per pile: the speed must equal the largest pile |
([30, 11, 23, 4, 20], 6) | 23 | |
([1000000000], 2) | 500000000 | |
([312884470], 312884469) | 2 | At speed 1 she'd need one hour too many |
(Confirm: h ≥ number of piles, so an answer always exists (speed = max pile); hours per pile = ceil(pile / k).)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in