Reported February 2024
Amazondynamic programming

Minimum Total Errors

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

You've got a binary string with '!' wildcards and you need to replace them to minimize adjacent '01' or '10' pairs. Amazon reported this in February 2024, and it's a classic dynamic programming setup. The trap is thinking greedy will work. It won't. You need to track state as you build the answer, and StealthCoder becomes your safety net if the DP transitions blur during the live OA.

The problem

\ See the Image Source section for the original statement :)\ \ In a vast digital database, numbers were carefully stored as strings of binary characters, '0' and '1'. But something went wrong. In place of some digits, mysterious '!' marks appeared, casting doubt on what those digits should be. Should they be '0's or '1's? To make matters worse, whenever a '0' and '1' pair appeared together, they caused glitches, small errors that multiplied throughout the system. Some combinations triggered more glitches than others. The challenge now is to replace all the '!' marks in a way that minimizes the total glitches, while keeping the system stable and efficient.\

Reported by candidates. Source: FastPrep

Pattern and pitfall

This is a dynamic programming problem where you decide for each '!' whether it should be '0' or '1'. The key insight is that each position's cost depends only on what comes before it. You define a state: dp[i][last_char] = minimum errors up to position i when the character at i is last_char. For each '!', you compute both branches (set to '0' or '1'), and for fixed characters, you have one branch. When computing transitions, count a glitch whenever the current character differs from the previous one. The trick candidates miss is forgetting to track which character you actually chose at the previous position, leading to wrong recurrence relations. StealthCoder handles the state transitions if you blank on the exact recurrence.

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 Minimum Total Errors 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

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as house robber. If you have time before the OA, drill that.

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

Minimum Total Errors FAQ

Is this really DP and not greedy?+

Yes. Greedy fails because a local choice (always pick '0' to avoid '01') creates worse downstream costs. DP considers all paths and picks the globally optimal one. The problem demands global optimization.

What are the exact states I need to track?+

Position and the last character placed (either '0' or '1'). So dp[i][0] and dp[i][1] represent minimum errors at position i when position i holds '0' or '1' respectively. If position i is a fixed character, only one state is valid.

How do I count the glitches?+

For each transition from position i-1 to i, if the character at i-1 differs from the character at i, you add 1 glitch. Build this into your recurrence: dp[i][c] = min(dp[i-1][prev] + cost) where cost is 1 if c != prev, else 0.

What's the space complexity and can I optimize it?+

Naive DP is O(n) space. You can optimize to O(1) by keeping only the previous row, since each state depends only on the prior position. This matters for large inputs and shows the interviewer you're thinking about efficiency.

Do I have to reconstruct the answer or just return the minimum cost?+

The problem likely asks for the minimum number of glitches, not the reconstructed string. Return the dp value at the end. If reconstruction is needed, backtrack through the DP table to find which choices led to the minimum.

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