Reported December 2023
Amazonmath

Warehouse Distribution

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

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

Amazon reported this Warehouse Distribution OA in December 2023, and it looks scarier than it is. Piles, moves, min-max difference. Your brain wants a simulation or a heap. Don't. It reduces to one division and one pass over the array. If you've got the OA in a day or two, this is a ten-minute problem once you see it. If you blank on the reduction, StealthCoder is the invisible safety net running during the live OA. But you can learn the trick right now, so read on.

The problem

Amazon has a warehouse that stores piles of boxes containing goods to be shipped. There are n piles numbered 1, 2,..., n, where the i-th pile has boxes[i] boxes.
To achieve an even distribution of boxes, the caretaker can perform the following operation any number of times (possibly zero):
Choose two distinct piles i and j such that boxes[i] > 0.
Remove one box from pile i and place it on pile j (increment boxes[j] by 1 and decrement boxes[i] by 1).
The caretaker wishes to minimize the difference between the maximum and the minimum number of boxes among the piles. Call this minimum achievable difference d.
Complete the function findMinimumOperations, which returns the minimum number of operations required to reach a configuration whose difference between the maximum and minimum number of boxes equals d.

Function
findMinimumOperations(boxes: int[]) → long

Examples
Example 1
boxes = [5, 5, 8, 7]
return = 2
Consider the number of piles to be n = 4 and the boxes in them are boxes = [5, 5, 8, 7]. The minimum possible difference that can be achieved is 1 by transforming the piles into [6, 6, 7, 6] as below. Hence the answer is 2.
Example 2
boxes = [2, 4, 1]
return = 1
Move a box from pile 2 to pile 3: [2, 4, 1] -> [2, 3, 2]
Example 3
boxes = [4, 4, 4, 4, 4]
return = 0

Constraints
1 <= n <= 105
1 <= boxes[i] <= 109

Reported by candidates. Source: FastPrep

Pattern and pitfall

Total boxes never change, so the best difference d depends only on sum and n. If sum % n == 0, every pile can hit sum/n and d = 0. Otherwise d = 1, with sum % n piles holding avg+1 and the rest holding avg. Example 1: sum 25, n 4, so three piles of 6 and one of 7. Now count moves. Each move shifts one box, so the answer is the total surplus above target. Sort descending, give the top r piles target avg+1 and the rest avg, then add up max(0, boxes[i] - target[i]). Pitfalls: sum overflows 32-bit with n up to 10^5 and values up to 10^9, so use 64-bit. Also don't give the extra box to arbitrary piles. Largest piles should keep the extra, or you overcount. If you freeze live, StealthCoder can hand you this solution from the screen, invisible to the proctor.

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 Warehouse Distribution 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

⏵ The honest play

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

Amazon 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.

Warehouse Distribution FAQ

What's the trick in Amazon's Warehouse Distribution problem?+

Moving boxes never changes the total. So the best possible difference is 0 if sum divides evenly by n, otherwise 1. Then the answer is the number of boxes that must leave piles sitting above their target. No simulation needed.

How hard is this OA really?+

Easy once you spot the math. The implementation is a sort plus one loop. The hard part is resisting the urge to simulate moves with a heap. Most of the difficulty is recognizing that the sum fixes the outcome.

Which piles get the extra box when sum % n is not zero?+

The largest ones. Sort descending, assign avg+1 to the first r piles where r = sum % n, and avg to the rest. That minimizes moves because those piles already hold the most boxes and lose the fewest.

Do I need 64-bit integers?+

Yes. With n up to 10^5 and each pile up to 10^9, the sum reaches 10^14. The function returns long for a reason. Use long in Java or C++, and watch intermediate sums in any typed language.

How do I prep for this in 48 hours?+

Write the solution once from scratch: sum, average, remainder, sort descending, accumulate surplus. Test it on [5,5,8,7], [2,4,1], and [4,4,4,4,4]. Then check edge cases like n = 1 and a remainder of zero. That covers it.

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

OA at Amazon?
Invisible during screen share
Get it