Make Array Distinct
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's March OA is asking candidates to make an array distinct, and you've got a problem with no explicit statement but a clear signal: this is about array manipulation under a cost constraint. You're probably given an array with duplicates and a budget to remove or increment elements. The trick is figuring out the minimum operations needed. If you blank on approach during the live OA, StealthCoder will read the exact constraints and surface the pattern in real time so you can code from that foundation instead of guessing.
Pattern and pitfall
The core pattern here is greedy or dynamic programming over an array, depending on whether you can increment elements freely or have a fixed cost. Most likely you're sorting the array first, then iterating through and ensuring each element is strictly greater than the previous one, incrementing as needed and tracking total cost. The pitfall is not considering that incrementing one element might cascade: you need each position to be at least previous[i] + 1. If costs are per-operation, a greedy left-to-right pass after sorting is optimal. If there's a budget constraint, you may need DP to find the minimum cost subset or minimum operations. StealthCoder will confirm the input format and constraints so you're not second-guessing yourself.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Make Array Distinct 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as minimum increment to make array unique. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Make Array Distinct FAQ
Is this a sorting problem or a DP problem?+
Almost certainly sorting plus greedy. Sort the array first, then iterate left to right ensuring each element is strictly greater than the previous, incrementing as needed. If there's a budget or cost constraint, DP might be needed, but start with greedy unless the problem explicitly asks for a constrained solution.
What's the common pitfall?+
Forgetting that incrementing element i might force element i+1 higher too. You can't just patch duplicates independently. Work left to right after sorting and track the minimum value each position must be, accounting for all prior increments.
How do I prepare in 48 hours?+
Know how to sort an array and do a single pass greedy adjustment. Practice one or two array manipulation problems on LeetCode. Understand the difference between modifying in place versus building a new array. That's enough to sketch a solution.
Is there a trick with negative numbers or zero?+
Possibly. If the array can have negatives, you might increment from the minimum value up. If zero is allowed, ensure your boundary logic is tight. Read the constraints carefully during the OA itself.
What if the array is already distinct?+
Return 0 or an empty result, depending on what the problem asks. The algorithm handles this naturally: if each element is already greater than the previous (after sorting), no increments are needed.