PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's skill in array algorithms and in-place algorithm design, emphasizing understanding of time and space complexity and handling edge cases when identifying the smallest missing positive integer.

  • Medium
  • Apple
  • Coding & Algorithms
  • Data Scientist

Find Smallest Missing Positive Integer in O(n) Time

Company: Apple

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Onsite

##### Scenario LeetCode-style algorithm phone interview ##### Question Implement a function that returns the smallest missing positive integer in an unsorted integer array in O(n) time and O( 1) space, then explain the complexity trade-offs. ##### Hints Use index placement/cyclic sort to achieve constant extra space.

Quick Answer: This question evaluates a candidate's skill in array algorithms and in-place algorithm design, emphasizing understanding of time and space complexity and handling edge cases when identifying the smallest missing positive integer.

Given an unsorted list of integers, return the smallest positive integer that does not appear in the list. Your algorithm must run in O(n) time and use O(1) extra space. You may modify the input list in place.

Constraints

  • 0 <= len(nums) <= 100000
  • -2147483648 <= nums[i] <= 2147483647
  • The solution must run in O(n) time.
  • The solution must use O(1) extra space, excluding the input array.

Examples

Input: ([1, 2, 0],)

Expected Output: 3

Explanation: The positive integers 1 and 2 are present, so the smallest missing positive integer is 3.

Input: ([3, 4, -1, 1],)

Expected Output: 2

Explanation: After ignoring non-positive values, 1 is present but 2 is missing.

Input: ([7, 8, 9, 11, 12],)

Expected Output: 1

Explanation: No value 1 appears in the array, so 1 is the smallest missing positive integer.

Input: ([],)

Expected Output: 1

Explanation: The empty array contains no positive integers, so the answer is 1.

Input: ([1, 1],)

Expected Output: 2

Explanation: Duplicate 1s should not cause an infinite swap loop. Since 1 is present and 2 is missing, the answer is 2.

Input: ([1, 2, 3, 4],)

Expected Output: 5

Explanation: All positive integers from 1 through 4 are present, so the smallest missing positive integer is 5.

Input: ([2],)

Expected Output: 1

Explanation: The only element is 2, so 1 is missing.

Hints

  1. Only values in the range 1 to n can affect the answer, where n is the length of the array.
  2. Try placing each valid number x at index x - 1 using swaps, similar to cyclic sort.
Last updated: Jun 21, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Minimum Cells to Bridge a Magic Grid - Apple (hard)
  • Find Common Prefix Across Strings - Apple (easy)
  • Find Minimum Processing Rate - Apple
  • Compute Earliest Bus Arrival - Apple (medium)
  • Find the Extra Edge - Apple (hard)