Make a Sorted Array Unique In Place
Reported by candidates from Alpaca's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The classic way to fumble this one is writing a fresh array or calling remove() inside a loop, then watching it fall apart on the in-place requirement. Alpaca reported this in August 2026: take a sorted integer array, compact the duplicates in place, and return the unique prefix. It's an easy array question, and the report says it was solved in a shared Google Doc, so nobody's running your code. That makes clean logic matter more than speed. If your mind goes blank on the pointer setup, StealthCoder sits invisibly on your screen during the live OA and hands you the solution as a safety net.
The problem
You are given an integer array nums sorted in nondecreasing order. Modify it in place so that each distinct value appears exactly once and the retained values stay in sorted order. Return an array containing exactly the compacted prefix. Extra capacity after that prefix may be left unchanged. What the interview report shared The interviewer asked an easy coding question: remove duplicate elements in a sorted array in place. The candidate solved it in a shared Google Doc. Function makeUniqueInPlace(nums: int[]) → int[] Examples Example 1 nums = [1,1,2] return = [1,2] The sorted values are 1, 1, and 2. After in-place compaction the unique prefix is [1,2]. Example 2 nums = [0,0,1,1,1,2,2,3,3,4] return = [0,1,2,3,4] Adjacent duplicates are overwritten so each retained value appears once. The compacted prefix is [0,1,2,3,4]. Example 3 nums = [] return = [] An empty array has no values to retain, so the compacted prefix is empty. Constraints 0 <= nums.length <= 10^5. -10^9 <= nums[i] <= 10^9. nums is sorted in nondecreasing order.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is two pointers. Keep a write index starting at 1 and scan with a read index from 1. Whenever nums[read] differs from nums[read-1], copy it to nums[write] and bump write. Because the array is sorted, duplicates are always adjacent, so comparing neighbors is enough. At the end, return the first write elements. The common pitfall is deleting elements mid-loop, which shifts everything and turns O(n) into O(n^2) at n up to 10^5. The second pitfall is forgetting the empty array: guard for length 0 and return [] before touching index 0. Also compare against the previous read value or the last written value, either works, but don't mix them up. In a shared doc with no test runner, trace Example 2 by hand before you finish. If you freeze live, StealthCoder is the hedge that gives you the pointer logic fast.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Make a Sorted Array Unique In Place 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
This OA pattern shows up on LeetCode as remove duplicates from sorted array. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Alpaca's OA.
Alpaca 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 a Sorted Array Unique In Place FAQ
How hard is the Alpaca sorted array dedupe question really?+
It's easy. The report calls it an easy coding question, and it's the standard two-pointer compaction. If you've seen it once, it's about ten lines. The only real risk is overthinking it or missing the empty array case.
What's the trick to solving it in place?+
Use a write pointer and a read pointer. Since the array is sorted, duplicates sit next to each other. Copy a value forward only when it differs from the previous one, then return the prefix up to the write pointer.
What edge cases should I check?+
Empty array returns an empty array. A single element returns itself. An array where every value is identical returns one element. Also negatives are fine since you only compare for equality, never do arithmetic on values.
Can I use a set to dedupe instead?+
A set breaks the in-place requirement and ignores that the input is sorted. It would also cost extra memory. Interviewers asking for in place want the two-pointer approach, so use it even if a set seems quicker to write.
How do I prepare for this in 48 hours?+
Write the two-pointer dedupe from memory three times, once with an empty input. Then practice explaining the loop out loud, since this was done in a shared doc where your reasoning is visible. That's enough for an easy problem like this.