Sort Colors
MediumThis is an extra practice problem. It doesn't have walkthrough slides. You can solve it with the pattern template you already know. Open the hints if you get stuck.
Question
You get a list that only has the values 0, 1, and 2. Sort it so all the 0s come first. The 1s go next, and the 2s go last.
Return the sorted list.
Input: nums = [2, 0, 2, 1, 1, 0]
Output: [0, 0, 1, 1, 2, 2]
Input: nums = [2, 0, 1]
Output: [0, 1, 2]
Input: nums = [0, 0, 1]
Output: [0, 0, 1]
The list is already sorted, so it stays the same.
Input: nums = [1]
Output: [1]
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
Given nums = [1, 2, 0, 2, 1], what does the low/mid/high pointer approach return?
[0, 0, 1, 1, 2]
[0, 1, 1, 2, 2]
[1, 1, 0, 2, 2]
[0, 1, 2, 1, 2]
Remember to understand the problem and plan your approach before you start coding.