Reported October 2024
Navantwo pointers

Sort Colors

Reported by candidates from Navan's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Navan OA. Under 2s to a working solution.
Founder's read

Navan reportedly dropped Sort Colors on candidates in October 2024, and the constraint line is the whole game: up to 100000 elements, only values 0, 1, 2, constant extra space, no library sort. That rules out the lazy answers fast. If your OA invite says Navan, expect this one or a close cousin. The pattern is the Dutch national flag, a three-pointer partition that finishes in one pass. It's a 15-line solution, and the risk is fumbling pointer logic under a timer. StealthCoder is the safety net running invisibly on the live OA if your mind goes blank on the swap rules.

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 three regions: zeros on the left, twos on the right, ones in the middle. Keep low, mid, and high pointers. If nums[mid] is 0, swap with low, then advance both. If it's 1, advance mid. If it's 2, swap with high and decrement high, but do NOT advance mid, because the swapped-in value is unchecked. That single mistake fails most attempts. A counting approach (count 0s, 1s, 2s, then overwrite) also works in two passes with O(1) space, since only three counters are needed. It's simpler and hard to botch, but the interviewer may want the one-pass version. Both are O(n) time. The constraint rules out a comparison sort at O(n log n) being acceptable in spirit, and the no-library rule blocks the shortcut. If you blank on the pointer order during the live OA, StealthCoder gives you the partition code to read off and verify.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as sort colors. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Navan's OA.

Navan reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Sort Colors FAQ

What's the trick in Navan's Sort Colors question?+

Use the Dutch national flag partition. Three pointers split the array into zeros, ones, and twos in one pass. Swap 0s to the front, 2s to the back, and let 1s fall into the middle. It runs in O(n) time with O(1) extra space.

Why can't I advance mid after swapping with high?+

The value you swap in from the high end hasn't been inspected. It could be a 0, 1, or 2. If you move mid forward, you skip it and leave a misplaced element. Only advance mid after swapping with low or seeing a 1.

Is the counting solution acceptable here?+

Usually yes. Count the 0s, 1s, and 2s, then overwrite the array in order. It uses three integers, so space is constant, and it's two passes. It doesn't call a library sort. If the grader only checks output and complexity, it passes.

How hard is Sort Colors really?+

It's a medium by label but easy once you've seen the partition idea. The code is short. Difficulty comes from pointer bookkeeping and edge cases like all identical values or a single element. Trace both examples by hand before submitting.

How do I prepare for this in 48 hours?+

Write the three-pointer version from memory twice, then the counting version once. Test on [2,0,1], [1], and [2,2,0,0]. That covers the common failure modes. Don't spend time on other sorting algorithms, since this OA bans the library sort and wants the partition.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Navan.

OA at Navan?
Invisible during screen share
Get it