Challenge - 5 Problems
Dutch Flag Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Dutch Flag Algorithm After One Pass
What is the state of the array after the first pass of the Dutch National Flag algorithm on the input array?
DSA Go
func sortColors(nums []int) { low, mid, high := 0, 0, len(nums)-1 for mid <= high { if nums[mid] == 0 { nums[low], nums[mid] = nums[mid], nums[low] low++ mid++ } else if nums[mid] == 1 { mid++ } else { nums[mid], nums[high] = nums[high], nums[mid] high-- } } } nums := []int{2, 0, 2, 1, 1, 0} sortColors(nums) fmt.Println(nums)
Attempts:
2 left
💡 Hint
Think about how the pointers low, mid, and high move and swap elements.
✗ Incorrect
The Dutch National Flag algorithm sorts the array in one pass by moving 0s to the front, 2s to the end, and 1s stay in the middle. After the full pass, the array is sorted as [0, 0, 1, 1, 2, 2].
🧠 Conceptual
intermediateWhy Use Three Pointers in Dutch Flag Algorithm?
Why does the Dutch National Flag algorithm use three pointers (low, mid, high) instead of just one or two?
Attempts:
2 left
💡 Hint
Think about how the algorithm divides the array during sorting.
✗ Incorrect
The three pointers help partition the array into three sections: all 0s before low, all 1s between low and mid, and all 2s after high, allowing sorting in one pass without extra space.
🔧 Debug
advancedIdentify the Bug in This Dutch Flag Implementation
What error will this code produce when sorting colors using the Dutch National Flag algorithm?
DSA Go
func sortColors(nums []int) { low, mid, high := 0, 0, len(nums)-1 for mid < high { if nums[mid] == 0 { nums[low], nums[mid] = nums[mid], nums[low] low++ mid++ } else if nums[mid] == 1 { mid++ } else { nums[mid], nums[high] = nums[high], nums[mid] high-- } } }
Attempts:
2 left
💡 Hint
Check the loop condition carefully and think about when the loop stops.
✗ Incorrect
The loop should run while mid <= high to ensure all elements are processed. Using mid < high skips checking the element at index high, causing incorrect sorting.
❓ Predict Output
advancedFinal Array After Partial Dutch Flag Execution
Given the array and the Dutch National Flag algorithm, what is the array state after the first swap operation?
DSA Go
nums := []int{1, 2, 0, 1, 2, 0} low, mid, high := 0, 0, len(nums)-1 if nums[mid] == 0 { nums[low], nums[mid] = nums[mid], nums[low] low++ mid++ } else if nums[mid] == 1 { mid++ } else { nums[mid], nums[high] = nums[high], nums[mid] high-- } fmt.Println(nums)
Attempts:
2 left
💡 Hint
Check the value at nums[mid] before the swap.
✗ Incorrect
Since nums[mid] is 1, the code only increments mid without swapping, so the array remains unchanged.
🚀 Application
expertNumber of Swaps in Dutch National Flag Algorithm
For the input array [2, 0, 2, 1, 1, 0], how many swaps does the Dutch National Flag algorithm perform to sort the array?
Attempts:
2 left
💡 Hint
Trace the swaps carefully during the sorting process.
✗ Incorrect
The algorithm swaps elements when moving 0s to the front and 2s to the end. For this input, it performs exactly 4 swaps.
