Challenge - 5 Problems
Dutch National Flag Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Dutch National Flag Partition on Array
What is the output of the following Go code that performs the Dutch National Flag partition on the array?
DSA Go
package main import "fmt" func dutchFlagPartition(arr []int, pivot int) { low, mid, high := 0, 0, len(arr)-1 for mid <= high { if arr[mid] < pivot { arr[low], arr[mid] = arr[mid], arr[low] low++ mid++ } else if arr[mid] == pivot { mid++ } else { arr[mid], arr[high] = arr[high], arr[mid] high-- } } } func main() { arr := []int{2, 0, 2, 1, 1, 0} dutchFlagPartition(arr, 1) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Focus on how the pointers low, mid, and high move and swap elements based on comparison with pivot.
✗ Incorrect
The algorithm partitions the array into three parts: less than pivot, equal to pivot, and greater than pivot. After processing, all 0s come first, then 1s, then 2s.
🧠 Conceptual
intermediateUnderstanding Pointers in Dutch National Flag Algorithm
In the Dutch National Flag algorithm, what is the role of the 'mid' pointer during the partitioning process?
Attempts:
2 left
💡 Hint
Think about which pointer moves forward to check elements one by one.
✗ Incorrect
The 'mid' pointer moves through the array from start to end, examining each element to decide if it should be swapped to the front, left as is, or swapped to the end.
🔧 Debug
advancedIdentify the Bug in Dutch National Flag Implementation
What error will occur when running this Go code for Dutch National Flag partition?
DSA Go
package main import "fmt" func dutchFlagPartition(arr []int, pivot int) { low, mid, high := 0, 0, len(arr) for mid <= high { if arr[mid] < pivot { arr[low], arr[mid] = arr[mid], arr[low] low++ mid++ } else if arr[mid] == pivot { mid++ } else { arr[mid], arr[high] = arr[high], arr[mid] high-- } } } func main() { arr := []int{2, 0, 2, 1, 1, 0} dutchFlagPartition(arr, 1) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Check the initialization of 'high' and how it is used as an index.
✗ Incorrect
The 'high' pointer is initialized to len(arr), which is out of bounds for indexing. Accessing arr[high] causes a runtime panic.
🚀 Application
advancedUsing Dutch National Flag to Sort Colors
Given an array representing colors as 0 (red), 1 (white), and 2 (blue), which option correctly sorts the array using Dutch National Flag algorithm?
DSA Go
func sortColors(nums []int) { low, mid, high := 0, 0, len(nums)-1 for mid <= high { switch { case nums[mid] == 0: nums[low], nums[mid] = nums[mid], nums[low] low++ mid++ case nums[mid] == 1: mid++ case nums[mid] == 2: nums[mid], nums[high] = nums[high], nums[mid] high-- } } }
Attempts:
2 left
💡 Hint
Check how the swaps and pointer movements organize the colors.
✗ Incorrect
The algorithm moves all 0s to the front, 2s to the end, and 1s remain in the middle, sorting the colors correctly.
🧠 Conceptual
expertTime Complexity of Dutch National Flag Algorithm
What is the time complexity of the Dutch National Flag algorithm for partitioning an array of size n?
Attempts:
2 left
💡 Hint
Consider how many times each element is processed during the partition.
✗ Incorrect
The algorithm uses three pointers and processes each element once, resulting in linear time complexity O(n).
