DSA · Dutch national flag · #28
Sort Colors
Module 31 · difficulty 2/5·⏱ 30:00starts on first keystroke
Given an array `nums` with `n` objects coloured red, white, or blue — represented by the integers `0`, `1`, and `2` — sort them in-place so that objects of the same colour are adjacent, in the order red, white, blue. Do not use a library sort. (Return value is ignored.)
Examples
nums = [2,0,2,1,1,0]→[0,0,1,1,2,2]nums = [2,0,1]→[0,1,2]nums = [0]→[0]
Stuck? Reveal an animated walkthrough of the approach.
Constraints
- · 1 <= nums.length <= 300
- · nums[i] is 0, 1, or 2.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.