First Missing Positive
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure that solves this Amazon OA question is the input array itself. First Missing Positive was reported in September 2026, and it punishes anyone who reaches for a hash set, because the O(1) extra space rule kills that approach. You're given an unsorted array and asked for the smallest positive integer that's missing, in O(n) time. The trick is old but it only feels obvious once you've seen it. If you blank mid-assessment, StealthCoder runs invisibly on your screen and gives you the solution as a safety net. Know the pattern first, though.
The problem
Given an unsorted integer array nums, return the smallest positive integer that does not appear in the array. Your algorithm must run in O(n) time and use O(1) extra space, excluding the input array. Function firstMissingPositive(nums: int[]) → int Examples Example 1 nums = [1,2,0] return = 3 The positive values 1 and 2 are present, so 3 is the first missing positive. Example 2 nums = [3,4,-1,1] return = 2 The value 1 is present but 2 is absent. Example 3 nums = [7,8,9,11,12] return = 1 No value 1 appears. Constraints 1 <= nums.length <= 100000. -2^31 <= nums[i] <= 2^31 - 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The answer is always in the range 1 to n+1, where n is the array length. So use the array as its own hash table. Walk through it and, for each value v between 1 and n, swap it into index v-1, repeating until the current slot holds an out-of-range value or the right value already sits at its target. Then scan once: the first index i where nums[i] != i+1 gives the answer i+1. If all match, return n+1. The classic pitfall is an infinite loop on duplicates, like [1,1]. Always compare nums[nums[i]-1] to nums[i] before swapping, not just the index. Another trap is ignoring negatives and huge values near 2^31, which just get skipped. Sorting is O(n log n) and a set breaks the space rule, so both fail the stated constraints. If the swap loop slips under pressure, StealthCoder on the live OA is your hedge.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill First Missing Positive 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 first missing positive. 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.
First Missing Positive FAQ
What's the trick for First Missing Positive?+
Cyclic placement. The answer must be in 1..n+1, so swap each value v in range into index v-1. After one pass, scan for the first index where nums[i] isn't i+1. That's your answer. It's O(n) time and O(1) space.
How hard is this really for an Amazon OA?+
It's labeled hard because of the space constraint, not the code. The final solution is about ten lines. If you've seen cyclic sort once, it's very doable. If you haven't, you'll likely burn time on sets and sorting.
Why does my swap loop hang forever?+
Duplicates. If nums[i] equals nums[nums[i]-1], swapping does nothing and you loop again. Check that the target slot doesn't already hold the same value before swapping, and use a while loop guarded by range checks.
Can I just use a hash set or sort the array?+
A set uses O(n) space and sorting is O(n log n). Both give correct answers but violate the stated constraints, so they may fail hidden checks or lose points. Mention them as baselines, then write the in-place version.
How do I prepare for this in 48 hours?+
Write the cyclic-swap solution from scratch three times. Test it on [1,2,0], [3,4,-1,1], [7,8,9,11,12] and [1,1]. Be able to explain why the answer is capped at n+1. That covers nearly every variation.