First Missing Positive
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reported this one in December 2020, and it looks friendlier than it is. First Missing Positive really reduces to one idea: the answer must land between 1 and n+1, so the array itself can act as your hash set. The O(n) time and O(1) space rule kills the easy set and sort approaches. If you have the OA in the next day or two, you need the in-place trick cold. StealthCoder is a desktop overlay that stays invisible during the live assessment, so it's a safety net if your mind goes blank on the swap loop. Read on for the pattern and the traps.
The problem
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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is cyclic placement. For an array of length n, the answer can only be 1 through n+1. Walk the array, and while nums[i] is in the range 1..n and isn't already sitting at index nums[i]-1, swap it into that slot. Then scan once more. The first index i where nums[i] != i+1 gives the answer i+1. If every slot matches, return n+1. The classic pitfall is the swap condition. Compare against the target slot's value, not nums[i] itself, or duplicates make you loop forever. Another trap is forgetting that negatives, zeros and huge values like 2^31-1 just get ignored. The inner while loop looks quadratic but each swap places one value permanently, so it's O(n). If you freeze on the duplicate check during the live OA, StealthCoder can hand you the clean version quietly.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill First Missing Positive cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as first missing positive. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
First Missing Positive FAQ
What's the trick for First Missing Positive?+
Use the array as its own hash table. The answer is always in 1..n+1, so swap each value x in that range to index x-1. Then scan for the first index where nums[i] isn't i+1. That gives O(n) time and O(1) extra space.
How hard is this one really?+
It's labeled hard, but it's one idea. Once you know cyclic placement, the code is about ten lines. The difficulty is coming up with it under the space constraint, so memorize the pattern rather than deriving it fresh in the OA.
Can I just use a set or sort the array?+
A set uses O(n) space and sorting is O(n log n), so both break the stated constraints. They'd give correct answers on small tests, but the assessment may judge against the requirement. Know the in-place method so you aren't forced into a fallback.
Why does the swap loop not run forever on duplicates?+
Because you only swap when the target slot doesn't already hold the same value. Check nums[nums[i]-1] != nums[i] before swapping. If they match, you skip. Without that guard, two equal values keep swapping with each other endlessly.
How do I prepare for this in 48 hours?+
Write the cyclic placement solution from memory three times. Test it on [1,2,0], [3,4,-1,1] and [7,8,9,11,12]. Add a duplicates case like [1,1] and a single-element case. That covers the edge cases Bloomberg-style tests tend to include.