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 TypeScript code that partitions the array around the pivot value 2 using the Dutch National Flag algorithm?
DSA Typescript
function dutchNationalFlagPartition(arr: number[], pivot: number): number[] {
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;
}
const input = [3, 2, 1, 2, 3, 1, 2];
console.log(dutchNationalFlagPartition(input, 2));Attempts:
2 left
💡 Hint
Remember the algorithm moves elements less than pivot to the front, equal in the middle, and greater to the end.
✗ Incorrect
The Dutch National Flag algorithm partitions the array into three parts: less than pivot, equal to pivot, and greater than pivot. After processing, the array becomes 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
Think about which pointer moves through the array checking elements.
✗ Incorrect
The 'mid' pointer moves from start to end, examining each element to decide if it should be swapped to the front, left in place, or swapped to the end.
🔧 Debug
advancedIdentify the bug in this Dutch National Flag implementation
What error will occur when running this TypeScript code for Dutch National Flag partition?
DSA Typescript
function dnfPartition(arr: number[], pivot: number): number[] {
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(dnfPartition([3, 2, 1, 2, 3, 1, 2], 2));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 partition.
🚀 Application
advancedApplying Dutch National Flag to sort colors
Given an array representing colors coded as 0 (red), 1 (white), and 2 (blue), which option shows the array after applying Dutch National Flag partition with pivot 1?
DSA Typescript
const colors = [2, 0, 2, 1, 1, 0]; function sortColors(arr: number[]): number[] { let low = 0, mid = 0, high = arr.length - 1; while (mid <= high) { if (arr[mid] < 1) { [arr[low], arr[mid]] = [arr[mid], arr[low]]; low++; mid++; } else if (arr[mid] === 1) { mid++; } else { [arr[mid], arr[high]] = [arr[high], arr[mid]]; high--; } } return arr; } console.log(sortColors(colors));
Attempts:
2 left
💡 Hint
The algorithm sorts elements less than pivot first, then equal, then greater.
✗ Incorrect
The array is sorted so that all 0s come first, then 1s, then 2s, resulting in [0, 0, 1, 1, 2, 2].
🧠 Conceptual
expertWhy Dutch National Flag algorithm is efficient for three-way partitioning?
Which statement best explains why the Dutch National Flag algorithm is efficient for partitioning arrays into three parts?
Attempts:
2 left
💡 Hint
Think about how many times the array is scanned and if extra memory is used.
✗ Incorrect
The algorithm uses three pointers to rearrange elements in one pass without extra arrays, making it efficient.
