Make Value Groups Contiguous
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You've got an Amazon OA from April 2026 asking you to make value groups contiguous by replacing occurrences of one integer with another. The problem sounds like a swap puzzle but it's actually a graph problem in disguise. You need to figure out the minimum number of merge operations to collapse all scattered values into single blocks. StealthCoder will help you spot the pattern if you freeze mid-solution.
The problem
You are given an integer array arr. In one operation, choose two values x and y, and replace every occurrence of x in the array with y. An array is called contiguous by value if, for every distinct value, all occurrences of that value appear in one uninterrupted block. Return the minimum number of operations needed to make the array contiguous by value. Function Description Complete the function minOperationsToMakeValuesContiguous in the editor below. minOperationsToMakeValuesContiguous has the following parameter: Returns int: the minimum number of replacement operations. Replace every 2 with 1 to get [1, 1, 1]. Every value already occupies a single contiguous block.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is recognizing this as a union-find or connected-components problem. Each value that appears scattered in the array is a separate component. When you replace all x with y, you're merging two components into one. The minimum operations equals the number of distinct values minus the number of connected components formed by the actual positions and adjacencies in the array. Build a graph where values that appear near each other or need to be merged are nodes, then count how many merge operations compress them into one. StealthCoder catches the moment you realize this isn't a greedy problem.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Make Value Groups Contiguous 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Make Value Groups Contiguous FAQ
Is this just counting how many values appear more than once?+
No. A value appearing twice doesn't automatically cost one operation. The cost depends on whether those occurrences are already contiguous and how they relate to other values. You need to model which values must merge to satisfy the contiguity constraint.
Can I solve this with a greedy left-to-right pass?+
Not reliably. Greedy choices about which value to replace first can lead to suboptimal paths. You need to see the full structure of which value groups are already contiguous and which must merge, then compute the minimum spanning tree or union-find solution.
What if every value appears in just one block already?+
Then the answer is zero. No operations needed. The array is already contiguous by value. This is your base case and a good sanity check when testing.
Does the final value matter, like which value everything becomes?+
No. The problem only asks for the minimum number of operations, not which value to use. You're counting merge steps, not tracking the target value. This simplifies the problem significantly once you see it.
How do I model which values need to merge?+
Two values need to merge if their occurrences interleave in the array. Scan left to right tracking which distinct values you've seen. If you see a value, then a different value, then the first value again, those two values must eventually merge to become contiguous.