Coding · should-tier · asked at Amazon, Apple, Google, Meta, Microsoft, Netflix
First Missing Positive
Problem
Given an unsorted integer array nums, return the smallest positive integer (1, 2, 3, ...) that does not appear in it. Aim for linear time and constant extra space; you may rearrange the input array.
- The array is non-empty and has at most 150,000 elements.
- Values are arbitrary signed 32-bit integers, so most of them may be negative, zero, or far larger than the array length.
Examples
nums = [4, 1, 2, 0]→3. 1 and 2 are present; 3 is the first gap.nums = [5, -3, 1, 6, 3]→2. 1 is present, 2 is not; the negative and the large values are irrelevant.nums = [20, 30, 40]→1. No small positives at all, so the answer is 1.
Hints
- The answer is always between 1 and
n + 1inclusive, where n is the array length. Why? - That bound means only values in
[1, n]matter; everything else can be ignored. - Put each value v in
[1, n]at indexv - 1by swapping. Afterwards, the first index i wherenums[i] != i + 1gives the answeri + 1.
Last reviewed 8 September 2026
Locked
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Secure payment by Stripe · instant access · refund questions: [email protected]
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.