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 One Pass
What is the output array after running one full pass of the Dutch National Flag algorithm on the input array?
DSA Java
int[] nums = {2, 0, 2, 1, 1, 0}; int low = 0, mid = 0, high = nums.length - 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--; } } System.out.println(java.util.Arrays.toString(nums));
Attempts:
2 left
💡 Hint
Remember the algorithm moves 0s to the front, 2s to the end, and 1s stay in the middle.
✗ Incorrect
The Dutch National Flag algorithm sorts the array in one pass by swapping 0s to the front and 2s to the end, leaving 1s 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 National Flag algorithm, what is the role of the 'mid' pointer during the sorting process?
Attempts:
2 left
💡 Hint
Think about which pointer moves forward to check each element.
✗ Incorrect
The 'mid' pointer moves through the array from left to right, checking each element to decide if it should be swapped to the front (0), left as is (1), or swapped to the end (2).
🔧 Debug
advancedIdentify the Bug in Dutch Flag Implementation
What error will occur when running this code snippet for sorting colors using the Dutch National Flag algorithm?
DSA Java
int[] nums = {2, 0, 1}; int low = 0, mid = 0, high = nums.length - 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--; } } System.out.println(java.util.Arrays.toString(nums));
Attempts:
2 left
💡 Hint
Check the loop condition carefully and how it affects processing the last element.
✗ Incorrect
The loop condition 'mid < high' stops before 'mid' reaches 'high', so the last element is not processed, causing incorrect sorting.
❓ Predict Output
advancedResult of Dutch Flag Algorithm on Custom Input
What is the output after sorting the array using the Dutch National Flag algorithm?
DSA Java
int[] nums = {1, 2, 0, 1, 2, 0, 1}; int low = 0, mid = 0, high = nums.length - 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--; } } System.out.println(java.util.Arrays.toString(nums));
Attempts:
2 left
💡 Hint
The algorithm sorts all 0s first, then 1s, then 2s.
✗ Incorrect
After the algorithm completes, the array is sorted with all 0s at the front, followed by 1s, then 2s at the end.
🧠 Conceptual
expertWhy Dutch National Flag Algorithm is O(n)
Why does the Dutch National Flag algorithm run in linear time O(n) despite having nested swaps inside the loop?
Attempts:
2 left
💡 Hint
Think about how many times each element is processed and how pointers move.
✗ Incorrect
Each element is processed once or twice at most, and the pointers move in one direction without revisiting elements, making the algorithm linear time.
