0
0
DSA Pythonprogramming~20 mins

Sort Colors Two Pointer Dutch Flag in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dutch Flag Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Dutch Flag Algorithm on Mixed Colors
What is the output of the following code after sorting the colors using the Dutch Flag algorithm?
DSA Python
def sortColors(nums):
    low, mid, high = 0, 0, len(nums) - 1
    while mid <= high:
        if nums[mid] == 0:
            nums[low], nums[mid] = nums[mid], nums[low]
            low += 1
            mid += 1
        elif nums[mid] == 1:
            mid += 1
        else:
            nums[mid], nums[high] = nums[high], nums[mid]
            high -= 1

nums = [2, 0, 2, 1, 1, 0]
sortColors(nums)
print(nums)
A[0, 0, 1, 1, 2, 2]
B[1, 0, 2, 0, 1, 2]
C[2, 2, 1, 1, 0, 0]
D[0, 1, 0, 1, 2, 2]
Attempts:
2 left
💡 Hint
Think about how the pointers low, mid, and high move and swap elements.
🧠 Conceptual
intermediate
1:00remaining
Number of Passes in Dutch Flag Algorithm
How many passes over the array does the Dutch Flag algorithm make to sort colors?
ADepends on the input size
BOne pass
CThree passes
DTwo passes
Attempts:
2 left
💡 Hint
Consider how the pointers move through the array.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Dutch Flag Implementation
What error will this code produce when run?
DSA Python
def sortColors(nums):
    low, mid, high = 0, 0, len(nums) - 1
    while mid < high:
        if nums[mid] == 0:
            nums[low], nums[mid] = nums[mid], nums[low]
            low += 1
            mid += 1
        elif nums[mid] == 1:
            mid += 1
        else:
            nums[mid], nums[high] = nums[high], nums[mid]
            high -= 1

nums = [2, 0, 1]
sortColors(nums)
print(nums)
AIndexError
B[0, 1, 2]
CSyntaxError
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
Predict Output
advanced
2:00remaining
Final Array State After Sorting
What is the final state of the array after running this code?
DSA Python
def sortColors(nums):
    low, mid, high = 0, 0, len(nums) - 1
    while mid <= high:
        if nums[mid] == 0:
            nums[low], nums[mid] = nums[mid], nums[low]
            low += 1
            mid += 1
        elif nums[mid] == 1:
            mid += 1
        else:
            nums[mid], nums[high] = nums[high], nums[mid]
            high -= 1

nums = [1, 2, 0, 1, 2, 0, 1]
sortColors(nums)
print(nums)
A[0, 0, 1, 1, 1, 2, 2]
B[0, 1, 0, 1, 2, 1, 2]
C[2, 2, 1, 1, 0, 0, 1]
D[1, 0, 2, 0, 1, 2, 1]
Attempts:
2 left
💡 Hint
Remember the order: all 0s first, then 1s, then 2s.
🧠 Conceptual
expert
1:30remaining
Why Dutch Flag Algorithm is Optimal for Sorting Colors?
Why is the Dutch Flag algorithm considered optimal for sorting an array containing only 0s, 1s, and 2s?
AIt sorts the array by counting each color and then overwriting
BIt uses recursion to divide and conquer the array
CIt sorts the array in one pass with constant extra space
DIt uses built-in sort which is optimized for small ranges
Attempts:
2 left
💡 Hint
Think about time complexity and space usage.