Unload warehouses in turns with limited skips
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's online assessment, last asked Sep 2025. It tests greedy, math and sorting.
You and a co-worker empty n warehouses, one warehouse at a time; warehouse i holds inventory[i] items. On each warehouse you take turns, you first: you dispatch dispatch1 items per turn, then your co-worker dispatches dispatch2. You earn one credit for a warehouse if it becomes empty (inventory 0 or below) right after your dispatch. You may ask your co-worker to skip a turn, and then you dispatch again at once, but only skips times in total across all warehouses. Return the most credits you can earn.
Interface
Unless you agree on a different interface with the interviewer, assume:
max_credits(inventory: list[int], dispatch1: int, dispatch2: int, skips: int) -> int
Example
max_credits([10, 20, 30], 5, 5, 1) -> 1
# every warehouse would be emptied by the co-worker; each needs one skip to be yours,
# and there is one skip
max_credits([4, 8, 12], 5, 3, 0) -> 2
# 4: you empty it at once. 12: 5, 3, then your 5 empties it. 8: 5, then the co-worker's 3.
max_credits([7, 14, 3, 9], 3, 4, 3) -> 3
# skips needed per warehouse: 2, 2, 0, 0; spend 2 of the 3 skips on one of the first two
Constraints
-
1 <= n <= 10^5;1 <= inventory[i], dispatch1, dispatch2, skips <= 10^9.
Clarifying questions
- Do you always dispatch first on each warehouse? (Yes.)
- When your co-worker skips, can several skips be used in a row on one warehouse? (Yes.)
- Is the order of warehouses fixed, and does it matter? (It does not change the answer.)
What a strong answer covers
- One full round removes
dispatch1 + dispatch2items, so only the last partial round matters: letr = inventory[i] mod (dispatch1 + dispatch2), or the full round when that is 0. You needceil(r / dispatch1) - 1skips so that your dispatch takes the last item. - Then a budget problem: sort the per-warehouse skip costs and take the cheapest ones while the budget lasts. O(n log n) time, O(n) space.
- No simulation turn by turn, which is far too slow with values up to 10^9.
- Warehouses that cost 0 skips, and 64-bit arithmetic.
Follow-ups
- Your co-worker may also ask you to skip, trying to earn credits themselves.
- Each warehouse's credit has a different value. What changes? (A knapsack.)
Where it's asked
- Amazon · Online assessment · Last asked Sep 2025Coding problems plus a repository bug-fix section with a built-in assistant or a design scenario, then a work-style survey · AI only inside the bug-fix section's built-in assistant
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
