Challenge - 5 Problems
Dutch Flag Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of the Dutch Flag sorting code?
Given the array
[2, 0, 2, 1, 1, 0], what will be the state of the array after running the Dutch Flag sorting algorithm below?DSA Javascript
function sortColors(nums) {
let low = 0, mid = 0, high = nums.length - 1;
while (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--;
}
}
return nums;
}
console.log(sortColors([2, 0, 2, 1, 1, 0]));Attempts:
2 left
💡 Hint
Think about how the pointers low, mid, and high move and swap elements.
✗ Incorrect
The algorithm partitions the array into three parts: 0s at the start, 1s in the middle, and 2s at the end. It swaps elements to ensure this order.
🧠 Conceptual
intermediateWhat is the role of the 'mid' pointer in the Dutch Flag algorithm?
In the Dutch Flag sorting algorithm, what does the 'mid' pointer represent during the sorting process?
Attempts:
2 left
💡 Hint
Consider which pointer moves through the array checking each element.
✗ Incorrect
The 'mid' pointer moves through the array from start to end, checking each element to decide if it should be swapped or skipped.
🔧 Debug
advancedIdentify the error in this Dutch Flag implementation
What error will this code produce when sorting colors using the Dutch Flag algorithm?
DSA Javascript
function sortColors(nums) {
let low = 0, mid = 0, high = nums.length - 1;
while (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--;
}
}
return nums;
}Attempts:
2 left
💡 Hint
Check the loop condition carefully and think about when the loop should stop.
✗ Incorrect
The loop should run while mid <= high to ensure all elements are processed. Using mid < high skips the last element, causing incorrect sorting.
🚀 Application
advancedHow to modify Dutch Flag algorithm to sort an array with four colors?
If you have an array with four colors represented by 0, 1, 2, and 3, how can you extend the Dutch Flag algorithm to sort it efficiently?
Attempts:
2 left
💡 Hint
Think about how the original algorithm uses pointers to separate three groups.
✗ Incorrect
By using three pointers, you can partition the array into four segments, moving elements to their correct positions in one pass.
❓ Predict Output
expertWhat is the output after sorting with a modified Dutch Flag algorithm?
Given the array
[1, 2, 0, 1, 2, 0, 1], what is the array state after running the code below?DSA Javascript
function sortColors(nums) {
let low = 0, mid = 0, high = nums.length - 1;
while (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--;
}
}
return nums;
}
console.log(sortColors([1, 2, 0, 1, 2, 0, 1]));Attempts:
2 left
💡 Hint
Trace the swaps carefully, especially how 2s are moved to the end.
✗ Incorrect
The algorithm moves all 0s to the front, 2s to the end, and leaves 1s in the middle, resulting in a sorted array.
