Sort Colors
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure behind Bloomberg's Sort Colors question is just the array itself, and that's the whole point. Reported in January 2022, this OA hands you an array of 0s, 1s and 2s and tells you to sort it in place with no library sort and constant extra space. It looks like a warmup. It's really a check on whether you know the three-pointer partition or fall back on counting and a second pass. If you've seen it, it takes five minutes. If you blank, StealthCoder runs invisibly during the live OA and gives you the pointer logic as a safety net.
The problem
You are given an integer array nums containing only 0, 1, and 2. Rearrange the array in place so that equal values are adjacent and the values appear in the order 0, 1, then 2. Do not call a library sorting routine. Return the same array after rearranging it so the result can be evaluated. Function sortColors(nums: int[]) → int[] Examples Example 1 nums = [2,0,2,1,1,0] return = [0,0,1,1,2,2] The two zeros come first, followed by the two ones and the two twos. Example 2 nums = [2,0,1] return = [0,1,2] Each color occurs once. Constraints 1 <= nums.length <= 100000 nums[i] is 0, 1, or 2. The rearrangement must use constant auxiliary space.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is the Dutch National Flag partition. Keep three pointers: low, mid and high. Low marks where the next 0 goes, high marks where the next 2 goes, and mid scans. If nums[mid] is 0, swap with low and advance both. If it's 1, just advance mid. If it's 2, swap with high and shrink high, but do NOT advance mid, because the swapped-in value is unchecked. That last point is the classic pitfall, and it breaks the output on inputs like [2,0,1]. The counting approach (tally 0s, 1s, 2s, then overwrite) also meets constant space and is fine, but it takes two passes, and an interviewer may push for one. Runtime is O(n) and space is O(1). With n up to 100000 nothing else matters. If your mind goes blank mid-assessment, StealthCoder is the hedge that shows the pointer swaps on screen without the proctor seeing it.
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 Sort Colors 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 sort colors. 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.
Sort Colors FAQ
How hard is Sort Colors really?+
It's an easy-to-medium problem. The logic is short, but the mid-pointer rule trips people up. Once you know why you don't advance mid after swapping with high, the code is about ten lines. Most failures come from off-by-one errors, not from the idea.
What's the trick to solving it in one pass?+
Use three pointers: low, mid, high. Zeros get swapped to the low side, twos get swapped to the high side, and ones stay in the middle. Only advance mid after a 0 or 1 swap. After a swap with high, recheck the same index.
Can I just count the zeros, ones and twos?+
Yes. Count each value, then overwrite the array with that many 0s, 1s and 2s. It uses constant space and passes the constraints. It takes two passes instead of one, which is fine unless the interviewer asks for single pass.
Is this pattern still asked by Bloomberg?+
This one was reported in January 2022 as a Bloomberg OA. Partitioning and two-pointer in-place problems keep showing up in assessments generally. Know the three-pointer version cold and you're covered for close variants like moving zeroes or partitioning around a pivot.
How do I prepare in 48 hours?+
Write the Dutch flag solution from memory three times. Then test it by hand on [2,0,1], [1], and an all-2s array. Those cases catch the mid-pointer bug. After that, spend your remaining time on other in-place array problems, not on more sorting theory.