Reported July 2026
Amazonprefix sum

Minimum Redistribution Cost

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

A running prefix-sum array of surplus and deficit is the whole solution to the Amazon "Minimum Redistribution Cost" question, reported in July 2026. Warehouses sit in a circle, items only move one direction, and each edge crossing costs 1. It looks like a greedy simulation, and that's the trap. Candidates who simulate item by item die on n up to 10^5 and values up to 10^9. Once you see the flow across each edge as a prefix sum, it's a few lines of code. If you blank during the live OA, StealthCoder runs invisibly on your screen and gives you the approach in real time. Know the trick first, though. It's short.

The problem

There are n warehouses arranged in a circle. Warehouse i initially stores products[i] items.
You may redistribute items around the circle, but all moved items must travel in one fixed direction: either clockwise or counter-clockwise. Moving one item across one edge costs 1.
Return the minimum total cost needed to make every warehouse contain the same number of products. You may choose the better of the two directions.

Function
getMinimumRedistributionCost(products: int[]) → long
Complete getMinimumRedistributionCost.
int products[n]: product counts around the circle
Returns long: the minimum redistribution cost.

Examples
Example 1
products = [1,11,1,1,1]
return = 20
The average is 3. The extra 8 products at the second warehouse must fill deficits of 2 at four other warehouses. In either direction, the total edge-crossing cost is 20.
Example 2
products = [0,6,0]
return = 6
The average is 2. Four items move out of the middle warehouse: two cross one edge and two cross two edges.

Constraints
1 <= products.length <= 10^5
0 <= products[i] <= 10^9
The total number of products is divisible by products.length.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Subtract the average from every count to get d[i]. Take prefix sums p[i]. In one fixed direction, the flow across edge i equals x + p[i], where x is a constant circulating amount around the circle. Flow can't be negative in a one-way setup, so x must be at least -min(p). Cost is the sum of all edge flows, which is sum(p) - n * min(p), minimized at x = -min(p). Run it once on the array, then once on the reversed array for the other direction, and return the smaller. Check example 2: p = [-2,2,0], sum 0, min -2, cost 6. That matches. Pitfalls: integer overflow (use 64-bit), forgetting the reverse pass, and trying to simulate moves. Prefix sums reach about 10^14. If the formula slips away mid-assessment, StealthCoder is the hedge that surfaces it while you keep typing.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Minimum Redistribution Cost 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Redistribution Cost FAQ

What's the trick in Amazon's Minimum Redistribution Cost problem?+

Convert counts to surplus or deficit relative to the average, then take prefix sums. Each prefix value is the net flow across an edge. Because the circle allows a constant circulating offset, you shift all flows so the smallest becomes zero. Cost is sum(p) - n * min(p). No heap or simulation needed.

Why do I need to run it in both directions?+

The problem lets you pick clockwise or counter-clockwise, and the costs differ. Compute the formula on the original array, then on the reversed array, and take the minimum. In example 1 and 2 both directions tie, but that won't hold for asymmetric inputs.

What's the expected time complexity?+

O(n) time and O(1) extra space if you accumulate prefix sums on the fly. With n up to 10^5, anything quadratic fails. Sorting isn't needed. Two linear passes, one per direction, are enough.

What bugs break most solutions here?+

Overflow is the big one. Prefix sums and the final cost can reach roughly 10^14, so use long or 64-bit ints everywhere, including n * min(p). Others: forgetting the reverse pass, computing the average with integer slips, and treating the circle like a line with no offset.

How do I prepare for this in 48 hours?+

Hand-verify both examples with the prefix formula until it's automatic. Then write the code once from memory with 64-bit types and test an array of length 1 and an already-balanced array. Both should return 0. That's about an hour of work, not a week.

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