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 with Mixed Colors
What is the output of the following Rust code that sorts colors represented by 0, 1, and 2 using the Dutch Flag algorithm?
DSA Rust
fn sort_colors(nums: &mut Vec<i32>) {
let (mut low, mut mid, mut high) = (0, 0, nums.len() - 1);
while mid <= high {
match nums[mid] {
0 => {
nums.swap(low, mid);
low += 1;
mid += 1;
}
1 => {
mid += 1;
}
2 => {
nums.swap(mid, high);
if high == 0 { break; }
high -= 1;
}
_ => {}
}
}
}
fn main() {
let mut colors = vec![2, 0, 2, 1, 1, 0];
sort_colors(&mut colors);
println!("{:?}", colors);
}Attempts:
2 left
💡 Hint
Remember the Dutch Flag algorithm sorts 0s, then 1s, then 2s in one pass.
✗ Incorrect
The algorithm uses three pointers: low, mid, and high. It swaps 0s to the front, 2s to the end, and leaves 1s in the middle. After processing, the vector is sorted as [0, 0, 1, 1, 2, 2].
🧠 Conceptual
intermediateNumber of Passes in Dutch Flag Algorithm
How many passes over the array does the Dutch Flag algorithm make to sort colors represented by 0, 1, and 2?
Attempts:
2 left
💡 Hint
Think about how the pointers move and how many times each element is visited.
✗ Incorrect
The Dutch Flag algorithm sorts the array in a single pass by using three pointers to partition the array into three parts.
🔧 Debug
advancedIdentify the Bug in Dutch Flag Implementation
What error will occur when running this Rust code for the Dutch Flag algorithm on an empty vector?
DSA Rust
fn sort_colors(nums: &mut Vec<i32>) {
let (mut low, mut mid, mut high) = (0, 0, nums.len() - 1);
while mid <= high {
match nums[mid] {
0 => {
nums.swap(low, mid);
low += 1;
mid += 1;
}
1 => {
mid += 1;
}
2 => {
nums.swap(mid, high);
high -= 1;
}
_ => {}
}
}
}
fn main() {
let mut colors = vec![];
sort_colors(&mut colors);
println!("{:?}", colors);
}Attempts:
2 left
💡 Hint
Check what happens when nums.len() is zero and you subtract 1.
✗ Incorrect
When nums is empty, nums.len() - 1 causes unsigned integer underflow, resulting in a very large number used as index, causing panic at index out of bounds.
📝 Syntax
advancedCorrect the Syntax Error in Dutch Flag Rust Code
Which option fixes the syntax error in this Rust code snippet for the Dutch Flag algorithm?
DSA Rust
fn sort_colors(nums: &mut Vec<i32>) {
let (mut low, mut mid, mut high) = (0, 0, nums.len() - 1)
while mid <= high {
match nums[mid] {
0 => {
nums.swap(low, mid);
low += 1;
mid += 1;
}
1 => mid += 1,
2 => {
nums.swap(mid, high);
high -= 1;
}
_ => {}
}
}
}Attempts:
2 left
💡 Hint
Rust requires semicolons after statements.
✗ Incorrect
The missing semicolon after the tuple assignment causes a syntax error. Adding it fixes the code.
🚀 Application
expertFinal State of Array After Dutch Flag Algorithm
Given the input array [1, 2, 0, 1, 2, 0, 1], what is the final state of the array after applying the Dutch Flag algorithm?
Attempts:
2 left
💡 Hint
The algorithm sorts all 0s first, then 1s, then 2s.
✗ Incorrect
The Dutch Flag algorithm partitions the array so that all 0s come first, followed by all 1s, then all 2s. The sorted array is [0, 0, 1, 1, 1, 2, 2].
