FastPrepFirst Missing Positive
Problems/First Missing Positive

First Missing Positive

Amazon logoAmazon● HardFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source provides the exact canonical First Missing Positive problem URL. The judged contract follows that canonical constant-extra-space exercise, with an estimated 99% source match.

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.

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
class Solution {
    public int firstMissingPositive(int[] nums) {
      // write your code here
    }
}
Checking account…