Reported April 2026
Amazongreedy

Minimum Cost to Convert Products to Variant A

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're looking at an Amazon OA from April 2026 that feels like a sliding window problem but hides a greedy trick. You have an inventory of 0s and 1s. Each operation picks a subarray of fixed length k, costs the sum of that subarray, and flips exactly one 1 to 0. The goal: convert everything to 0 with minimum total cost. StealthCoder reads the problem and feeds you the pattern in real time if you freeze up mid-OA.

The problem

An inventory array product contains only 0 and 1, where 0 represents variant A and 1 represents variant B. In one operation, choose a subarray of length k. The cost of that operation is the sum of the values inside the chosen subarray. Then choose one index inside that subarray whose value is 1 and change it to 0. Return the minimum total cost needed to convert every product to variant A. Function Description Complete the function minCostToConvertAllToVariantA in the editor below. minCostToConvertAllToVariantA has the following parameters: Returns int: the minimum total cost. Use window [0, 1] twice, then window [1, 2] once. The costs are 2 + 1 + 1 = 4. Choose the left window once to clear the first 1, and the right window once to clear the last 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

This is a greedy optimization disguised as a window-sliding problem. The key insight: every 1 must be flipped exactly once, and you choose which window pays for it. For each 1, you want to pick the window covering it that has the lowest sum. Precompute all possible k-length windows and their costs, then for each 1, greedily select the cheapest window that contains it. The catch is that a single window can flip only one 1 per operation, so if a window contains multiple 1s, you may use it multiple times. Build a cost map per window position and iterate through 1s left to right, picking the cheapest available window each time. StealthCoder surfaces this greedy choice when you're stuck on the enumeration logic.

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 Cost to Convert Products to Variant A 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 Cost to Convert Products to Variant A FAQ

What does 'choose one index inside that subarray whose value is 1 and change it to 0' mean?+

You pick a k-length window, pay the sum of its values, then flip exactly one 1 inside it to 0. You don't flip all 1s in the window, just one. That's why you might use the same window multiple times if it contains multiple 1s.

Is this a sliding window or greedy problem?+

Both. You slide to find all windows of length k and their costs. Then you greedily assign each 1 to the cheapest window covering it. It's not dynamic programming, it's greedy assignment with a preprocessing phase.

Can the same window be used multiple times?+

Yes. If a window has multiple 1s, you perform the operation multiple times. Each operation flips one 1, so if a window covers three 1s, you use it up to three times and pay its cost each time.

How do I handle the k parameter? Is it given or do I infer it?+

The problem statement should specify k or it's implied by context. Check the full function signature. If k is variable, the problem becomes NP-hard, so assume k is fixed and given.

What's the time complexity I should aim for?+

Precompute k-length windows in O(n). For each 1, find the cheapest window covering it in O(k) or O(1) if you precompute a cost table per index. Total should be O(n * k) or O(n) depending on space trade-offs.

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