Challenge - 5 Problems
Dutch Flag Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Think about how the pointers low, mid, and high move and swap elements.
✗ Incorrect
The Dutch Flag algorithm sorts the array in one pass by moving 0s to the front, 2s to the end, and 1s stay in the middle.
🧠 Conceptual
intermediate1:00remaining
Number of Passes in Dutch Flag Algorithm
How many passes over the array does the Dutch Flag algorithm make to sort colors?
Attempts:
2 left
💡 Hint
Consider how the pointers move through the array.
✗ Incorrect
The algorithm uses three pointers and sorts the array in a single pass by swapping elements as needed.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check the loop condition carefully.
✗ Incorrect
The loop condition should be 'mid <= high' to ensure all elements are processed. Using 'mid < high' can cause an infinite loop.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
Remember the order: all 0s first, then 1s, then 2s.
✗ Incorrect
The algorithm sorts the array so that all 0s come first, then 1s, then 2s.
🧠 Conceptual
expert1: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?
Attempts:
2 left
💡 Hint
Think about time complexity and space usage.
✗ Incorrect
The Dutch Flag algorithm sorts the array in a single pass and uses only a few pointers, so it uses constant extra space and linear time.