Reported August 2023
Amazondynamic programming

Find Minimum Inefficiency

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 an Amazon OA in August with a string optimization problem disguised as server configuration. You're given a mix of fixed servers (0 or 1) and wildcards (?), and you need to assign each wildcard to minimize adjacent mismatches. This is a dynamic programming problem where you track the best cost ending in each state as you move left to right. StealthCoder will catch the transition logic if you freeze up on the recurrence.

The problem

Amazon Web Service has n servers, each of them either has high fault tolerance or high reliability. A system works better if all the servers have the same attributes. The inefficiency of a group of server is defined as the number of adjacent pairs of server that have different attributes. Consider, for example, a set of servers described as 1001001 where '0' means the server has high fault tolerance, '1' means the server has high reliabiity. The inefficiency of this group is 4 as described in the image below: Given a string serverType of length n
consisting of '0', '1', and '?', where '0' means the server has high fault tolerance, '1' means the server has high reliability, and '?' means you can install any
type of server there, find the minimum inefficiency you can get after install a server at each '?'.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is DP with state compression. As you iterate through the string, maintain the minimum inefficiency for assignments that end in 0 or 1 at each position. For a fixed character, you have one choice. For a wildcard, you pick both 0 and 1, compare the cost of transitioning from the previous state, and keep only the cheapest path to each ending value. The inefficiency increases by 1 whenever adjacent characters differ. Most candidates overshoot by trying greedy or BFS when a simple two-variable DP pass solves it in O(n) time. If you blank on the recurrence during the OA, StealthCoder reads the problem and feeds you the transition formula.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Find Minimum Inefficiency 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. If you're reading this with an OA window open, you're who this was built for.

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. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Find Minimum Inefficiency FAQ

Is this really just 'make adjacent pairs match as much as possible'?+

Yes. Count the 0-1 boundaries. Your goal is to minimize that count by choosing what goes in the '?' slots. It's a single-pass DP problem, not a search problem.

Do I need to try all 2^k combinations of wildcards?+

No. That's exponential and will TLE. DP builds the optimal assignment as you go. At each position, you only care about the cheapest way to end in 0 or 1, not every path that got you there.

What's the edge case that catches people?+

Consecutive wildcards. If you have '??', you can make them both 0, both 1, or mixed. DP handles this naturally by computing all three options at each step and pruning the expensive ones.

How do I code the transition from position i to i+1?+

If s[i+1] is fixed, assign it and add 1 to cost if it differs from your previous choice. If it's a wildcard, try both 0 and 1 and pick the cheaper one. Store dp[i+1][0] and dp[i+1][1] as you go.

Do I need to reconstruct which wildcard gets which value?+

No. The problem only asks for minimum inefficiency, the number itself. Return the smaller of dp[n][0] and dp[n][1].

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