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 After Sorting
What is the output of the array after running the Dutch Flag sorting algorithm on the input array?
DSA C++
void sortColors(vector<int>& nums) { int low = 0, mid = 0, high = nums.size() - 1; while (mid <= high) { if (nums[mid] == 0) { swap(nums[low], nums[mid]); low++; mid++; } else if (nums[mid] == 1) { mid++; } else { swap(nums[mid], nums[high]); high--; } } } int main() { vector<int> nums = {2, 0, 2, 1, 1, 0}; sortColors(nums); for (int num : nums) { cout << num << " "; } return 0; }
Attempts:
2 left
💡 Hint
Remember the Dutch Flag algorithm sorts 0s, then 1s, then 2s in one pass.
✗ Incorrect
The algorithm uses three pointers to sort the array in one pass. It moves all 0s to the front, 2s to the end, and 1s remain in the middle. The final sorted array is [0, 0, 1, 1, 2, 2].
🧠 Conceptual
intermediateUnderstanding Pointer Roles in Dutch Flag Algorithm
In the Dutch Flag algorithm for sorting colors, what is the role of the 'mid' pointer?
Attempts:
2 left
💡 Hint
Think about 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 to the front (0) or back (2), or left in place (1).
🔧 Debug
advancedIdentify the Bug in Dutch Flag Implementation
What error will occur when running this code snippet for the Dutch Flag algorithm?
DSA C++
void sortColors(vector<int>& nums) { int low = 0, mid = 0, high = nums.size() - 1; while (mid < high) { if (nums[mid] == 0) { swap(nums[low], nums[mid]); low++; mid++; } else if (nums[mid] == 1) { mid++; } else { swap(nums[mid], nums[high]); high--; } } }
Attempts:
2 left
💡 Hint
Check the loop condition carefully and how it affects processing all elements.
✗ Incorrect
The loop condition 'mid < high' stops the loop before 'mid' reaches 'high', so the last elements are not processed, causing incorrect sorting.
❓ Predict Output
advancedFinal Array State After Partial Dutch Flag Steps
Given the initial array and partial steps of the Dutch Flag algorithm, what is the array state after the first swap?
DSA C++
vector<int> nums = {2, 0, 1, 2, 0, 1}; int low = 0, mid = 0, high = nums.size() - 1; if (nums[mid] == 2) { swap(nums[mid], nums[high]); high--; } // What is the array now?
Attempts:
2 left
💡 Hint
Swap the first and last elements because nums[mid] is 2.
✗ Incorrect
Swapping nums[0] (2) with nums[5] (1) results in [1, 0, 1, 2, 0, 2].
🧠 Conceptual
expertWhy Does Dutch Flag Algorithm Use Three Pointers?
Why does the Dutch Flag algorithm use three pointers (low, mid, high) instead of just one or two?
Attempts:
2 left
💡 Hint
Think about how the algorithm divides the array during sorting.
✗ Incorrect
The three pointers help partition the array into three sections: 0s at the front, 1s in the middle, and 2s at the end, all in one pass without extra space.
