Find the Smallest Unassigned Positive Locker ID

Last updated: November 29, 2026

Quick Overview

Given an unsorted array of integer locker IDs currently assigned at an Amazon Locker site (may contain negatives, zeros and duplicates), return the smallest positive ID that is not assigned, in O(n) time and O(1) extra space.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given an unsorted array of integer locker IDs currently assigned at an Amazon Locker site (may contain negatives, zeros and duplicates), return the smallest positive ID that is not assigned, in O(n) time and O(1) extra space.

An Amazon onsite hard question about allocating the next free locker slot. The interviewer usually accepts a set-based answer quickly, then asks for constant extra space, looking for in-place cyclic placement and a careful loop that does not spin forever on duplicates.

What the Interviewer Expects
  • Note the answer is always in the range 1 to n + 1
  • Give the hash set solution and its space cost
  • Place each value v at index v - 1 with in-place swaps
  • Guard against infinite loops on duplicates
  • Scan for the first index whose value is wrong
  • Prove the total number of swaps is O(n)
Key Topics to Cover
Cyclic Sort
In-Place Hashing
Arrays
Index Marking
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if you are not allowed to modify the input array?
  • How would you support assign and release operations with fast next-free queries?
  • How would you find the k smallest missing IDs?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list ids of length n with arbitrary integers. Output: the smallest integer >= 1 not present. Constraints: O(n) time, O(1) extra space, the input may be modified....

High-Level Design

With n numbers, the answer lies in [1, n + 1], so only values in that range matter. Use the array itself as a hash table: value v belongs at index v - 1. For each index, keep swapping its value into...


Submit Your Answer
Markdown supported