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 a sample array
What is the output of the following Rust code that partitions an array using the Dutch National Flag algorithm with pivot = 2?
DSA Rust
fn dutch_national_flag(arr: &mut [i32], pivot: i32) {
let mut low = 0;
let mut mid = 0;
let mut high = arr.len() - 1;
while mid <= high {
if arr[mid] < pivot {
arr.swap(low, mid);
low += 1;
mid += 1;
} else if arr[mid] == pivot {
mid += 1;
} else {
arr.swap(mid, high);
if high == 0 { break; } // prevent underflow
high -= 1;
}
}
}
fn main() {
let mut arr = [3, 2, 1, 2, 3, 1, 2];
dutch_national_flag(&mut arr, 2);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Remember the algorithm groups elements less than pivot first, then equal, then greater.
✗ Incorrect
The Dutch National Flag algorithm rearranges the array so that all elements less than the pivot come first, followed by elements equal to the pivot, then elements greater than the pivot. Here, 1 < 2, so 1s come first, then all 2s, then 3s.
🧠 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 through the array to check each element.
✗ Incorrect
The 'mid' pointer moves through the array from start to end, examining each element to decide if it should be swapped to the less than, equal to, or greater than section.
🔧 Debug
advancedIdentify the error in this Dutch National Flag implementation
What error will this Rust code produce when run, and why?
DSA Rust
fn dutch_national_flag(arr: &mut [i32], pivot: i32) {
let mut low = 0;
let mut mid = 0;
let mut high = arr.len() - 1;
while mid <= high {
if arr[mid] < pivot {
arr.swap(low, mid);
low += 1;
mid += 1;
} else if arr[mid] == pivot {
mid += 1;
} else {
arr.swap(mid, high);
high -= 1;
}
}
}
fn main() {
let mut arr = [2, 0, 1];
dutch_national_flag(&mut arr, 1);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Check the initial value of 'high' and how it is used as an index.
✗ Incorrect
Array indices go from 0 to arr.len() - 1. Initializing 'high' to arr.len() causes an out-of-bounds access when swapping.
❓ Predict Output
advancedResult of Dutch National Flag partition with all elements equal to pivot
What is the output of this Rust code when all elements are equal to the pivot?
DSA Rust
fn dutch_national_flag(arr: &mut [i32], pivot: i32) {
let mut low = 0;
let mut mid = 0;
let mut high = arr.len() - 1;
while mid <= high {
if arr[mid] < pivot {
arr.swap(low, mid);
low += 1;
mid += 1;
} else if arr[mid] == pivot {
mid += 1;
} else {
arr.swap(mid, high);
high -= 1;
}
}
}
fn main() {
let mut arr = [5, 5, 5, 5];
dutch_national_flag(&mut arr, 5);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
If all elements equal pivot, the array remains unchanged.
✗ Incorrect
Since all elements equal the pivot, the algorithm only increments 'mid' without swapping, so the array stays the same.
🚀 Application
expertNumber of swaps in Dutch National Flag partition
Given the array [3, 1, 2, 2, 1, 3, 2] and pivot = 2, how many swaps does the Dutch National Flag algorithm perform to partition the array?
Attempts:
2 left
💡 Hint
Trace the algorithm step-by-step counting swaps.
✗ Incorrect
Swaps occur when elements less than or greater than pivot are moved. Counting carefully yields 4 swaps.
