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 an array
What is the output of the following C++ code that partitions the array using the Dutch National Flag algorithm with pivot = 1?
DSA C++
void dutchFlagPartition(int pivot, std::vector<int>& arr) { int smaller = 0, equal = 0, larger = arr.size() - 1; while (equal <= larger) { if (arr[equal] < pivot) { std::swap(arr[smaller++], arr[equal++]); } else if (arr[equal] == pivot) { equal++; } else { std::swap(arr[equal], arr[larger--]); } } } int main() { std::vector<int> arr = {2, 0, 2, 1, 1, 0}; dutchFlagPartition(1, arr); for (int x : arr) std::cout << x << " "; return 0; }
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 running, all 0s (less than 1) come first, then all 1s, then all 2s.
🧠 Conceptual
intermediateUnderstanding the pointers in Dutch National Flag algorithm
In the Dutch National Flag algorithm, what is the role of the 'equal' pointer during the partitioning process?
Attempts:
2 left
💡 Hint
Think about which pointer moves through the array checking each element.
✗ Incorrect
The 'equal' pointer scans the array from left to right, classifying each element and swapping as needed to partition the array.
🔧 Debug
advancedIdentify the bug in this Dutch National Flag implementation
What error will this C++ code produce when run, and why?
DSA C++
void dutchFlagPartition(int pivot, std::vector<int>& arr) { int smaller = 0, equal = 0, larger = arr.size(); while (equal < larger) { if (arr[equal] < pivot) { std::swap(arr[smaller++], arr[equal++]); } else if (arr[equal] == pivot) { equal++; } else { std::swap(arr[equal], arr[larger--]); } } }
Attempts:
2 left
💡 Hint
Check the initial value of 'larger' and how it is used as an index.
✗ Incorrect
The 'larger' pointer is initialized to arr.size(), which is out of bounds for zero-based indexing. Accessing arr[larger] causes runtime error.
🚀 Application
advancedApplying Dutch National Flag to sort colors
Given an array representing colors as integers 0 (red), 1 (white), and 2 (blue), which option shows the array after applying Dutch National Flag partition with pivot = 1?
DSA C++
std::vector<int> colors = {2, 0, 1, 2, 1, 0}; dutchFlagPartition(1, colors); // Output the array
Attempts:
2 left
💡 Hint
The algorithm groups all 0s first, then 1s, then 2s.
✗ Incorrect
The Dutch National Flag algorithm sorts the array so that all 0s come first, then 1s, then 2s.
❓ Predict Output
expertFinal state of pointers after partitioning
After running the Dutch National Flag partition on array {1, 2, 0, 1, 2, 0} with pivot = 1, what are the final values of pointers 'smaller', 'equal', and 'larger'?
DSA C++
void dutchFlagPartition(int pivot, std::vector<int>& arr, int& smaller, int& equal, int& larger) { smaller = 0; equal = 0; larger = arr.size() - 1; while (equal <= larger) { if (arr[equal] < pivot) { std::swap(arr[smaller++], arr[equal++]); } else if (arr[equal] == pivot) { equal++; } else { std::swap(arr[equal], arr[larger--]); } } } int main() { std::vector<int> arr = {1, 2, 0, 1, 2, 0}; int smaller, equal, larger; dutchFlagPartition(1, arr, smaller, equal, larger); std::cout << smaller << " " << equal << " " << larger; return 0; }
Attempts:
2 left
💡 Hint
Trace the pointers as elements are swapped and moved.
✗ Incorrect
After partitioning, 'smaller' and 'equal' point just past the last element less than or equal to pivot, 'larger' points to the last element greater than pivot.
