Challenge: Dutch National Flag Problem

Let's solve the famous problem of Dutch National Flag.

Problem statement

Implement a function that sorts a list of 0s0s, 1s1s and 2s2s. This is called the Dutch National Flag Problem. Since the number OO can be represented by the blue color, 11 by the white color, and 22 as the red color, the task is to transform the list input to the Dutch flag.

Try solving this problem in-place and in linear time without using any extra space.


An array of 0s0s, 1s1s, and 2s2s


An array where the numbers 00, 11, and 22 are sorted

Sample input

lst = [2, 0, 0, 1, 2, 1, 0]

Sample output

result = [0, 0, 0, 1, 1, 2, 2]

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.