Reported March 2024
Airbnbbinary search

Minimum Eating Speed

Reported by candidates from Airbnb's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Airbnb OA. Under 2s to a working solution.
Founder's read

Airbnb reported this one in March 2024, and it's the classic Koko eating bananas problem wearing a Halloween costume. The data structure the solution hinges on isn't a fancy one. It's a sorted range of candidate speeds, from 1 up to the biggest pile, and you binary search that range. If your OA is in the next few days, this is the pattern to lock in. And if your mind goes blank mid-assessment, StealthCoder runs invisibly on screen as a safety net so you still ship a clean answer.

The problem

Your daughter, Alex, has just come home with a bag full of candy after a long night of trick-or-treating. Before going to sleep, Alex places the candy in numPiles piles with the i-th pile containing candyPiles[i] number of candies.
After arranging the candies into piles, Alex announces she is going to sleep for numHours hours.
Your plan is to eat all the candy before Alex wakes up in numHours. You can eat c candies per hour, but in each hour you will only eat candy from a single pile. If a pile contains fewer than c candies, you will only eat the number of candies in that pile and you will wait until the next hour to eat more candy.
Having a little bit of self-restraint your goal is to calculate the smallest number of candies c you need to eat per hour in order to finish all the candy before Alex wakes up again.

Function
minimumEatingSpeed(candyPiles: int[], numHours: int) → int
Complete the function minimumEatingSpeed in the editor.
minimumEatingSpeed has the following parameters:
int[] candyPiles: an array of integers representing the number of candies in each pile
int numHours: the number of hours Alex will be asleep for
Returns
int: the smallest number of candies c that you need to eat per hour in order to finish all the candies before Alex wakes up

Examples
Example 1
candyPiles = [4, 9, 11, 17]
numHours = 8
return = 6
To finish all the candies in 8 hours, you can eat at the following speed:
Hour 1: Eat 6 candies from the pile with 17 candies, 11 remain.
Hour 2: Eat 6 candies from the pile with 11 candies, 5 remain.
Hour 3: Eat 5 candies from the pile with 5 candies, 0 remain.
Hour 4: Eat 6 candies from the pile with 9 candies, 3 remain.
Hour 5: Eat 3 candies from the pile with 3 candies, 0 remain.
Hour 6: Eat 4 candies from the pile with 4 candies, 0 remain.
Hour 7: Eat 6 candies from the pile with 11 candies, 5 remain.
Hour 8: Eat 5 candies from the pile with 5 candies, 0 remain.
The smallest number of candies you need to eat per hour is 6.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: the answer c lives between 1 and max(candyPiles). For any speed c, hours needed is the sum of ceil(pile / c) across piles. That sum only shrinks as c grows, so it's monotonic, which means binary search on c. Set lo = 1, hi = max pile. Compute mid, total the hours, and if hours <= numHours, record mid and move hi down. Otherwise move lo up. The common pitfall is floor division. Use (pile + c - 1) / c for the ceiling, or you'll undercount hours. Another one is searching on the pile array itself instead of the speed range. Overflow can bite with large piles, so keep the hour sum in a wide integer. Complexity is O(n log max). If you blank during the live OA, StealthCoder is the hedge that hands you the binary search template fast.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Minimum Eating Speed cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as koko eating bananas. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Airbnb's OA.

Airbnb reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Eating Speed FAQ

What's the trick to Minimum Eating Speed?+

Binary search on the answer, not on the array. The speed c ranges from 1 to the largest pile. For each guess, sum ceil(pile / c) and compare to numHours. Fewer hours means the speed is fast enough, so try smaller. More hours means go bigger.

How hard is this Airbnb OA question really?+

Medium. The code is short, about 15 lines. The hard part is spotting that the feasibility check is monotonic, which makes binary search valid. Once you see that, it's mechanical. Most misses come from off-by-one bounds or ceiling math.

Why can't I eat from two piles in one hour?+

The problem says each hour you only eat from a single pile. So a pile of 5 at speed 6 still costs a full hour. That's why you use ceiling division per pile instead of dividing the total candy by c.

What should my search bounds be?+

Low is 1 and high is the max pile. Speed above the max pile never helps because each pile already takes one hour. Since numHours is at least the number of piles, the max pile is always a feasible upper bound.

How do I prep this in 48 hours?+

Write the binary search on answer template from memory twice. Then do the same shape on a couple of variants, like shipping packages in D days. Practice the ceiling formula and the lo/hi update rule until they're automatic.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Airbnb.

OA at Airbnb?
Invisible during screen share
Get it