Find the Smallest Unused Positive Work Item ID in Place
Last updated: November 29, 2026
Quick Overview
Given an unsorted array of Azure DevOps work item IDs (integers, may include negatives, zeros and duplicates), return the smallest positive ID not present, using O(n) time and O(1) extra space.
Microsoft
November 29, 20260
0
0 solved
Given an unsorted array of Azure DevOps work item IDs (integers, may include negatives, zeros and duplicates), return the smallest positive ID not present, using O(n) time and O(1) extra space.
A classic Hard problem in Microsoft onsite loops, here framed as allocating the next free ID. A hash set solution is quick, so the interviewer pushes for constant extra space and checks that you can reason about index-as-hash cyclic placement and its termination.
What the Interviewer Expects
- Start with the O(n) space set solution and state its cost
- Observe the answer lies in 1..n+1
- Place each value v at index v-1 by swapping
- Guard against infinite loops on duplicates
- Scan for the first mismatched index
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Why does the while loop terminate in O(n) total swaps?
- How would you do it if mutating the input is not allowed?
- 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 ProblemsSample Answer
Requirements
Input: list of n integers, any values. Output: smallest integer >= 1 not in the list. Must run in O(n) time and O(1) auxiliary space; mutating the input is allowed....
High-Level Design
Key fact: with n slots, the answer is at most n+1, since only values 1..n can occupy the slots. So treat the array itself as a hash table where value v belongs at index v-1. For each index, while the ...