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 code that partitions the array around the pivot value 1 using the Dutch National Flag algorithm?
DSA Javascript
function dutchFlagPartition(arr, pivot) {
let low = 0, mid = 0, high = arr.length - 1;
while (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--;
}
}
return arr;
}
console.log(dutchFlagPartition([2, 0, 2, 1, 1, 0], 1));Attempts:
2 left
💡 Hint
Think about how the algorithm moves elements less than, equal to, and greater than the pivot.
✗ Incorrect
The Dutch National Flag algorithm partitions the array into three parts: less than pivot, equal to pivot, and greater than pivot. The final array is sorted in that order.
🧠 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
Consider how the algorithm processes each element one by one.
✗ Incorrect
The 'mid' pointer scans the array from start to end, deciding whether to swap elements or move forward depending on their relation to the pivot.
🔧 Debug
advancedIdentify the Bug in Dutch National Flag Implementation
What error will the following code produce when run, and why?
DSA Javascript
function dutchFlagPartition(arr, pivot) {
let low = 0, mid = 0, high = arr.length - 1;
while (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--;
}
}
return arr;
}
console.log(dutchFlagPartition([1, 0, 1, 1, 1, 0], 1));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 the last element(s), causing incorrect output.
❓ Predict Output
advancedResult of Partitioning with Different Pivot
What is the output of the following code when partitioning the array around pivot value 2?
DSA Javascript
function dutchFlagPartition(arr, pivot) {
let low = 0, mid = 0, high = arr.length - 1;
while (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--;
}
}
return arr;
}
console.log(dutchFlagPartition([3, 2, 1, 2, 3, 1, 2], 2));Attempts:
2 left
💡 Hint
Remember the order: less than pivot, equal to pivot, greater than pivot.
✗ Incorrect
The algorithm rearranges the array so that all elements less than 2 come first, then all 2s, then all elements greater than 2.
🧠 Conceptual
expertTime Complexity of Dutch National Flag Algorithm
What is the time complexity of the Dutch National Flag algorithm when partitioning an array of size n?
Attempts:
2 left
💡 Hint
Think about how many times each element is processed.
✗ Incorrect
The algorithm uses three pointers and processes each element once, resulting in linear time complexity.
