Reported July 2026
Amazongreedy

Minimum Contiguous Replacements

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

Amazon reported this one in July 2026, and the name hides what it is. "Minimum Contiguous Replacements" sounds like a simulation or a search over merge orders. It isn't. It reduces to merging overlapping intervals and counting groups. Each value has a first and last position, and any value sitting inside another value's span has to end up in the same group. Once you see that, the code is a single pass. If you're taking this OA in the next day or two, learn that reduction cold. If your mind goes blank on the live assessment, StealthCoder runs invisibly on your desktop and can hand you the approach while you type.

The problem

You are given an array arr of integers. In one operation, choose two distinct values x and y that currently appear in the array, then replace every occurrence of x with y.
Return the minimum number of operations needed to make the array valid.
An array is valid if every distinct value forms exactly one contiguous block. Values do not all need to become the same.
For example, [1,1,2,2,3] is valid, while [1,2,1,3] is not valid because value 1 appears in two separated blocks.

Function
minOperations(arr: int[]) → int
Complete minOperations.
int arr[n]: the array to transform
Returns int: the minimum number of replacement operations.

Examples
Example 1
arr = [1,2,1]
return = 1
Replace every occurrence of 2 with 1, producing [1,1,1].
Example 2
arr = [1,2,3,1,2,3]
return = 2
One valid sequence is to replace all 2s with 1, then replace all 3s with 1.
Example 3
arr = [1,2,1,2]
return = 1
Replacing every occurrence of either value with the other makes the array one contiguous block.

Constraints
1 <= arr.length <= 1000
1 <= arr[i] <= 1000

Reported by candidates. Source: FastPrep

Pattern and pitfall

Here's the trick. For each distinct value, record its first and last index. A value x is broken up only if some other value sits between two of its occurrences, and the only fix is to put that value in x's group, so their spans must merge. Chain this and you get connected components of overlapping spans. Each operation reduces the distinct count by one, and each component collapses to one value, so the answer is distinct values minus components. Count components with a sweep: track the max last index seen, and when index i passes it, start a new component. The pitfall is simulating replacements or trying every pair, which is wasted effort at n up to 1000. Another one is forgetting that touching spans like [0,2] and [3,5] are separate. Check your sweep against example 2, where everything chains into one component and the answer is 2. StealthCoder is your hedge on the live OA if the interval framing doesn't come to you.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Minimum Contiguous Replacements 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 passed his OA cold and still thinks the filter is broken.

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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Contiguous Replacements FAQ

What's the real trick in Minimum Contiguous Replacements?+

Treat each value as an interval from its first to last index. Overlapping intervals must merge into one group, because anything inside a value's span has to share its label. The answer is distinct values minus the number of merged interval groups. No simulation needed.

How hard is this Amazon OA question really?+

Medium on paper, easy once you spot the interval reduction. The code is about fifteen lines. The hard part is trusting that you don't need to choose merge order or direction. Replacing x with y or y with x gives the same group count, so only the components matter.

Why is the answer distinct minus components?+

Every operation merges two values into one, so it lowers the distinct count by exactly one. A component of k overlapping values needs k-1 merges to become one block. Summing over components gives total distinct minus component count. Example 2 has 3 distinct and 1 component, so the answer is 2.

What edge cases should I test before submitting?+

Test a single-element array, which returns 0. Test an already valid array like [1,1,2,2,3], which also returns 0. Test adjacent but non-overlapping spans, and a value nested fully inside another like [1,2,2,1]. Also test all-identical values. These catch most off-by-one mistakes in the sweep.

How do I prepare for this in 48 hours?+

Practice interval merging until it's automatic: first and last index maps, then a sweep with a running max end. Write this exact solution once from memory, then run the three examples by hand. Spend the rest of your time on other array and greedy patterns, since this reduction shows up in disguise often.

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