Skip to interview prep
Apexed

66. Sort 0s, 1s and 2s (Dutch National Flag)

MediumSorting

nums contains only 0, 1 and 2. Sort it in one pass with O(1) extra space and return it.

Example 1

Input: nums = [2, 0, 2, 1, 1, 0]
Output: [0, 0, 1, 1, 2, 2]

Constraints

  • 1 ≤ nums.length ≤ 300

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Sort 0s, 1s and 2s (Dutch National Flag) — coding interview practice — Apexed