Get Minimum Amount
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in September 2026, and the detail that matters is the cost rule: changing every product of quality x to y costs exactly the count of x. You have an array like [7, 7, 5, 7, 3, 5, 3] and the answer is 4. If your OA lands in the next day or two, you need the pattern fast, not a lecture. It's an interval merging problem dressed up as warehouse inventory. StealthCoder is the safety net if your mind goes blank mid-assessment, but the idea below is short enough to hold in your head.
The problem
The manager of the Amazon warehouse has decided to make changes to the inventory. Currently, the inventory has n products, where the quality of the ith product after quality checks is represented by the array element quality[i]. The manager wants to create an optimal inventory, where the array of products quality follows the following property: All occurrences of each quality value must be contiguous. In order to convert the inventory into an optimal inventory, the manager can do the following operation any number of times: Choose two quality values x and y. Replace every product with quality x to have quality y instead. This operation costs num_replacements units of money, where num_replacements is the number of products whose quality was changed. Given n products and an array quality, find the minimum amount of money the manager has to spend to convert the inventory into an optimal inventory. Note: The quality of a product can be negative indicating that the product is of poor quality. Function getMinAmount(quality: int[]) → int Complete the function getMinAmount in the editor below. getMinAmount has the following parameter(s): int quality[n]: the quality of products Returns int: the minimum amount of money the manager has to spend to convert the inventory into an optimal inventory. Examples Example 1 quality = [7, 7, 5, 7, 3, 5, 3] return = 4 Given n = 7, quality = [7, 7, 5, 7, 3, 5, 3]. One of the optimal ways to convert is explained below: Hence, the total amount spent is 4.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Every value has a first and last index, so it covers an interval. If two value intervals overlap, the values can't both stay contiguous, so they have to be merged into one value. Merging means relabeling whole values, and each relabel costs the count of the value you change. Group the overlapping intervals into connected components. Inside a component, keep the value with the highest frequency and relabel all the others, so the cost is component size minus max frequency. Sum that over components. The pitfall is greedy relabeling of adjacent elements, which ignores that the operation changes every occurrence at once. Another trap is forgetting chains, where A overlaps B and B overlaps C. Sweep left to right tracking the farthest last index to find component boundaries. It runs in O(n) with a hash map. If you blank live, StealthCoder can hand you the sweep, but you should recognize the shape first.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Get Minimum Amount 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 StealthCoderRelated leaked OAs
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.
Get Minimum Amount FAQ
What's the trick in Get Minimum Amount?+
Treat each quality value as an interval from its first to last index. Overlapping intervals chain into components. In each component you must end with one value, so keep the most frequent one and pay for the rest. Answer is the sum of size minus max frequency per component.
How hard is this Amazon OA question really?+
Medium. The code is short, but seeing that overlaps chain into one component is the hard part. Candidates who try local greedy swaps get wrong answers. Once you think in intervals and frequencies, it's a single pass with a hash map.
How do I check my approach on the example?+
For [7, 7, 5, 7, 3, 5, 3], the 7 spans indexes 0-3, the 5 spans 2-5, the 3 spans 4-6. They chain into one component of size 7. Max frequency is 3 (the 7s). Cost is 7 minus 3, which equals 4. That matches the expected output.
What time complexity should I aim for?+
O(n). Build first index, last index, and count per value with hash maps. Then scan the array once, tracking the farthest last index seen in the current component. When the index reaches that bound, close the component and add its cost.
How do I prepare for this in 48 hours?+
Practice interval merging and grouping by first and last occurrence. Write the sweep by hand on two or three arrays, including a chain case and an already-contiguous case. Also test negative values and a single-element array, since quality can be negative.