Challenge - 5 Problems
Dutch Flag Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Dutch Flag Algorithm After One Pass
What is the state of the array after the first complete pass of the two-pointer Dutch Flag algorithm on the input array?
DSA C
void sortColors(int* nums, int numsSize) { int low = 0, mid = 0, high = numsSize - 1; while (mid <= high) { if (nums[mid] == 0) { int temp = nums[low]; nums[low] = nums[mid]; nums[mid] = temp; low++; mid++; } else if (nums[mid] == 1) { mid++; } else { int temp = nums[mid]; nums[mid] = nums[high]; nums[high] = temp; high--; } } } int main() { int nums[] = {2, 0, 2, 1, 1, 0}; int size = 6; sortColors(nums, size); // Print array after sorting for (int i = 0; i < size; i++) { printf("%d ", nums[i]); } return 0; }
Attempts:
2 left
💡 Hint
Trace the pointers low, mid, and high as the algorithm swaps elements.
✗ Incorrect
The Dutch Flag algorithm sorts the array in one pass by moving 0s to the front, 2s to the end, and 1s stay in the middle. After the full pass, the array is sorted as [0, 0, 1, 1, 2, 2].
🧠 Conceptual
intermediate1:30remaining
Number of Swaps in Dutch Flag Algorithm
Given an array of n elements containing only 0s, 1s, and 2s, what is the maximum number of swaps the two-pointer Dutch Flag algorithm will perform in the worst case?
Attempts:
2 left
💡 Hint
Consider that each element is swapped at most once or twice.
✗ Incorrect
Each element is moved at most once to its correct position, so the maximum swaps are proportional to n, the array size.
🔧 Debug
advanced2:00remaining
Identify the Bug in Dutch Flag Implementation
What error will the following code produce when sorting colors using the Dutch Flag algorithm?
DSA C
void sortColors(int* nums, int numsSize) { int low = 0, mid = 0, high = numsSize - 1; while (mid < high) { if (nums[mid] == 0) { int temp = nums[low]; nums[low] = nums[mid]; nums[mid] = temp; low++; mid++; } else if (nums[mid] == 1) { mid++; } else { int temp = nums[mid]; nums[mid] = nums[high]; nums[high] = temp; high--; } } }
Attempts:
2 left
💡 Hint
Check the loop condition carefully and how it affects the pointers.
✗ Incorrect
The loop condition should be 'mid <= high' to ensure all elements are processed. Using 'mid < high' skips processing when mid equals high, leaving the array partially unsorted.
❓ Predict Output
advanced2:00remaining
Final Array State After Partial Dutch Flag Execution
What is the array state after executing the Dutch Flag algorithm up to the point where mid pointer reaches index 3 on the input array?
DSA C
int nums[] = {2, 0, 1, 2, 0, 1}; int low = 0, mid = 0, high = 5; while (mid <= high) { if (nums[mid] == 0) { int temp = nums[low]; nums[low] = nums[mid]; nums[mid] = temp; low++; mid++; } else if (nums[mid] == 1) { mid++; } else { int temp = nums[mid]; nums[mid] = nums[high]; nums[high] = temp; high--; } if (mid == 3) break; }
Attempts:
2 left
💡 Hint
Simulate each step and watch how low, mid, and high pointers move.
✗ Incorrect
After processing until mid reaches 3, the array partially sorted is [0, 0, 1, 2, 2, 1].
🧠 Conceptual
expert1:30remaining
Why Dutch Flag Algorithm is Optimal for Sorting Colors
Why is the two-pointer Dutch Flag algorithm considered optimal for sorting an array containing only 0s, 1s, and 2s?
Attempts:
2 left
💡 Hint
Think about the number of passes and extra memory used.
✗ Incorrect
The Dutch Flag algorithm sorts the array in one pass using only a few pointers and no extra arrays, making it time and space efficient.
