Reported December 2025
Amazondivide and conquer

Minimum S3 Storage 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 December 2025, and it looks scarier than it is. Files 1 to 2^n, some sensitive, and you pick between storing a block whole or halving it. Underneath, it's a recursion over a segment tree built on the sensitive indices, and you only ever recurse where sensitive files exist. If you've got an OA invite for Amazon this week, this is the pattern to recognize. StealthCoder is the safety net if you blank mid-assessment, but the idea below is short enough to hold in your head.

The problem

A batch contains files numbered from 1 to 2^n. Some files are sensitive and require encryption; their indices are given in sensitiveFiles.
For any contiguous batch of M files:
If it contains X > 0 sensitive files, storing the whole batch costs M * X * encCost.
If it contains no sensitive files, storing the whole batch costs flatCost.
If the batch size is even, you may either store the whole batch or split it into two equal contiguous batches and pay the sum of their optimal costs. Return the minimum possible storage cost modulo 1_000_000_007.

Function
minStorageCost(n: int, encCost: int, flatCost: int, sensitiveFiles: int[]) → int

Examples
Example 1
n = 2
encCost = 2
flatCost = 1
sensitiveFiles = [1,3]
return = 6
Splitting all the way to single files costs 2 + 1 + 2 + 1 = 6, which is better than keeping the full batch or only splitting once.
Example 2
n = 3
encCost = 2
flatCost = 1
sensitiveFiles = [1,2,3,4,5,6,7,8]
return = 16
Every file is sensitive. Splitting into single files gives eight batches, each costing 2.
Example 3
n = 3
encCost = 2
flatCost = 1
sensitiveFiles = [7,1]
return = 8
One optimal split is [1], [2], [3,4], [5,6], [7], and [8], for total cost 8.

Constraints
1 <= n <= 3 * 10^5
1 <= encCost, flatCost <= 10^5
1 <= sensitiveFiles.length <= 2^n
Each sensitive file index is between 1 and 2^n.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: n can reach 3*10^5, so 2^n is astronomically large and you can't walk the range. But sensitiveFiles has limited length. Any segment with zero sensitive files costs flatCost, and splitting it never helps, since two flat halves cost 2*flatCost. So recurse only into segments that contain sensitive files, and answer empty segments in O(1). Count sensitive files in a segment by sorting the indices and using binary search, or by partitioning the sorted list as you recurse. For each segment, cost = min(M*X*encCost, cost(left)+cost(right)). Size-1 segments can't split. The pitfall is computing M*X*encCost with huge M. Take the modulo for the final answer, but compare true minimums carefully, because a modded value can't be compared reliably. Use big numbers or cap the values. If you freeze on that comparison during the live OA, StealthCoder can cover you.

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 S3 Storage 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 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.

Minimum S3 Storage Cost FAQ

What's the core trick in Minimum S3 Storage Cost?+

Don't touch the full 2^n range. Sort the sensitive indices and recurse only into halves that contain sensitive files. Empty halves cost flatCost immediately. That keeps the work proportional to the sensitive count times depth, not the range size.

How do I count sensitive files in a segment fast?+

Sort the indices once, then use binary search for the segment bounds, so the count is the difference of two positions. Or pass down the sub-slice of the sorted list as you split. Either way it's cheap per node.

Why not just modulo everything as I go?+

Because you're taking a min. Modded values don't preserve order, so comparing them can pick the wrong branch. Compare true costs, using big integers or a safe cap, and apply the modulo only to the final result.

Is splitting an empty segment ever useful?+

No. An empty segment costs flatCost whole, and splitting gives two empty halves costing 2*flatCost, which is worse. So stop recursing the moment a segment has no sensitive files.

How do I prepare for this in 48 hours?+

Practice divide-and-conquer recursion over index ranges with sorted lookups. Work through the three examples by hand, especially example 3, to see where splitting wins. Then code it once, watching recursion depth and the overflow-safe comparison.

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