Reported September 2026
Amazonprefix sum

Find Minimum 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

Amazon reported this one in September 2026, and the detail that trips people up is the rule that every transfer must go in one fixed direction around the circle. Containers sit in a ring, you need every one to end at the average, and moving one product across one edge costs 1. Pick clockwise or counterclockwise, then price the whole thing. It's a circular prefix-sum problem dressed up as a warehouse story. If you blank on the setup during the OA, StealthCoder sits invisibly on your screen as a safety net, but the idea below is short enough to memorize tonight.

The problem

A warehouse has n identical containers arranged in a circle. Adjacent containers are one unit apart, and the goal is to make every container hold the same number of products.
Choose either clockwise or counterclockwise movement; all transfers must use that same direction.
Products may be collected from containers with excess units and delivered to containers with deficits.
Moving one product across one edge costs 1.
Return the minimum total transfer cost. It is guaranteed that equal redistribution is possible.

Function
findMinimumCost(products: int[]) → long

Examples
Example 1
products = [3, 4, 6, 6, 6]
return = 7
Consider a circular arrangement of containers. The units in each container are products = [3, 4, 6, 6, 6].
Position 1
Position 2
Position 3
Position 4
Position 5
3 Products
4 Products
6 Products
6 Products
6 Products
Option 1:
Start at the 3rd position and move clockwise. Collect one product each from the 3rd, 4th, and 5th positions.
Transfer the products from:
the 5th position to the 1st position, the cost is 1.
the 4th position to the 1st position, the cost is 2.
the 3rd position to the 2nd position, the cost is 4.
Now each container has 5 units and the total cost is 1 + 2 + 4 = 7.
Option 2:
Start at the 5th position moving anti-clockwise. Collect one product each from the 5th, 4th, and 3rd positions.
Transfer the product from:
the 3rd position to the 1st position, the cost is 2.
the 4th position to the 1st position, the cost is 3.
the 5th position to the 2nd position, the cost is 3.
Now each container has 5 units and the total cost is 2 + 3 + 3 = 8. Return 7, the minimum cost achievable.
Example 2
products = [1, 11, 1, 1, 1]
return = 20
The final average is 3. One container has 8 extra products. Moving in either fixed direction, send 2 products to each of the other four containers, for total cost 2*1 + 2*2 + 2*3 + 2*4 = 20.

Constraints
1
0
The answer may exceed the 32-bit integer range.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Compute the average, then turn each container into a delta (products[i] minus average). In one fixed direction, surplus flows forward like a conveyor belt. Build the running prefix sum of deltas. Each prefix value is how many products must cross the edge after that position. But a circle has no natural start, and the cost depends on where the flow is cut. Rotate the start and recompute, or use the prefix sums with a shift. Pick the best start, then repeat for the reverse direction and take the smaller answer. The cost is the sum of absolute values of the shifted prefixes, and the best shift is the median of the prefix values. Pitfall: the answer overflows 32-bit, so use long. Also check Example 2 by hand: it gives 20. StealthCoder is your hedge if the circular part blanks you live.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Find Minimum 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find Minimum Cost FAQ

What's the trick in Amazon's Find Minimum Cost?+

Convert each container to its surplus or deficit versus the average, then take prefix sums. Each prefix is the number of products crossing an edge. Because it's a circle, you must choose where to cut the ring, and the best cut comes from the median of the prefix sums.

Why do I need to try both directions?+

The problem forces all transfers to go clockwise or all counterclockwise. The costs differ, as Example 1 shows with 7 versus 8. Compute the minimum cost for each direction, for example by reversing the array, and return the smaller one.

Why does the return type need to be long?+

The statement says the answer may exceed the 32-bit integer range. Large counts multiplied by distances add up fast. Use 64-bit for the average, the prefix sums, and the total, or you'll fail the big hidden tests.

How hard is this really?+

Medium. The prefix-sum idea is easy, but the circular cut and the one-direction rule catch people. If you've seen the line-version of balancing candies or gas stations, you're most of the way there. Walk Example 2 by hand to confirm 20.

How do I prepare in 48 hours?+

Write the linear version first: deltas, prefix sums, sum of absolute values. Then add the circular shift using the median trick. Test on both examples, including the [1, 11, 1, 1, 1] case that returns 20. Then handle the reverse direction and use long throughout.

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