Reported August 2025
Amazongreedy

Minimize Binary Subsequence 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 August 2025, and the detail that matters is in the cost rule: every "01" subsequence costs x and every "10" subsequence costs y, counted across all pairs, not just adjacent ones. That's a string with '!' wildcards and a modulo, so it looks scarier than it is. The pattern is greedy with prefix counts, and the answer is a clean split point. If you blank during the OA, StealthCoder runs invisibly on your desktop and can hand you the approach live.

The problem

You are given a string binaryString consisting only of '0', '1', and '!', and two integers x and y.
Replace every '!' with either '0' or '1'. After replacement, every subsequence equal to "01" contributes cost x, and every subsequence equal to "10" contributes cost y.
Return the minimum possible total cost modulo 1_000_000_007.

Function
minimizeBinarySubsequenceCost(binaryString: String, x: int, y: int) → int

Examples
Example 1
binaryString = "101!1"
x = 2
y = 3
return = 9
Replacing '!' with '0' gives cost 15. Replacing it with '1' gives cost 9, which is optimal.
Example 2
binaryString = "!!!!!"
x = 2
y = 3
return = 0
Replace all characters with the same bit, so there are no "01" or "10" subsequences.

Constraints
1 <= binaryString.length <= 10^5
0 <= x, y <= 10^5
binaryString contains only '0', '1', and '!'.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Here's the trick. Replacing '!' characters is not independent per character, but the optimal assignment has structure. Each '!' contributes cost based on how many 0s and 1s sit to its left and right. Choose the bit for position i, and its cost is linear in the prefix and suffix counts. The exchange argument says the optimal fill is a prefix of '!' set to one bit and the remaining suffix set to the other. So try every split point, maintain running counts of 0s and 1s, and update cost incrementally in O(n). Check both orientations: 0s then 1s, and 1s then 0s. The common pitfall is counting subsequences as adjacent pairs, or brute forcing 2^k fills. Another is overflow, so use 64-bit integers and take the modulo only at the end, since you're comparing minimums. Example 2 confirms it: all the same bit gives zero. StealthCoder is the hedge if the incremental update derivation slips under pressure.

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 Minimize Binary Subsequence 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.

Minimize Binary Subsequence Cost FAQ

How hard is Minimize Binary Subsequence Cost really?+

Medium-hard. The statement is simple, but you have to see that the '!' fill has a split structure and that subsequence costs come from prefix and suffix counts. Once you see it, the code is short and linear.

What's the core trick?+

Each '!' choice affects cost through counts of 0s and 1s on both sides. The optimal assignment is a prefix of one bit and a suffix of the other. Sweep all split points, keeping running counts, and take the minimum over both orientations.

Why not just try both values for each '!'?+

With up to 10^5 characters, that's 2^k fills for k wildcards, far too slow. Even a per-character greedy can fail because choices interact through the pair counts. You need the split-point insight to get linear time.

Where does the modulo 1_000_000_007 come in?+

Costs can get huge: up to about n^2/4 pairs times 10^5. Compare candidates using exact 64-bit values, then apply the modulo to the final minimum. Taking the modulo early can make you pick the wrong minimum.

How do I prepare in 48 hours for this Amazon OA?+

Practice counting subsequences with prefix counts, then write an incremental cost update as you move a split point. Test with all '!' strings, no '!' at all, and x or y equal to 0. Those edge cases catch most bugs.

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