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
Given the input array
[2, 0, 2, 1, 1, 0], what is the state of the array after the first complete pass of the Dutch Flag two-pointer algorithm?DSA C#
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--; } break; // simulate only one pass iteration }
Attempts:
2 left
💡 Hint
Focus on what happens when nums[mid] == 2 during the first iteration.
✗ Incorrect
The first iteration swaps nums[mid] (2) with nums[high] (0), resulting in [0, 0, 2, 1, 1, 2].
🧠 Conceptual
intermediateUnderstanding Pointer Roles in Dutch Flag Algorithm
In the Dutch Flag two-pointer algorithm for sorting colors, what is the main 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 from start to end, checking each element to decide if it should be swapped or skipped.
🔧 Debug
advancedIdentify the Bug in Dutch Flag Implementation
What error will the following code produce when sorting colors using the Dutch Flag algorithm?
DSA C#
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--; } }
Attempts:
2 left
💡 Hint
Check the loop condition and how mid and high pointers move.
✗ Incorrect
The loop condition should be mid <= high. Using mid < high causes the loop to miss processing when mid == high, resulting in incorrect sorting.
❓ Predict Output
advancedFinal Sorted Array After Dutch Flag Algorithm
What is the final sorted array after running the Dutch Flag algorithm on
[1, 0, 2, 1, 0]?DSA C#
int[] nums = {1, 0, 2, 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--; } }
Attempts:
2 left
💡 Hint
Remember the algorithm sorts 0s first, then 1s, then 2s.
✗ Incorrect
The algorithm moves all 0s to the front, 1s in the middle, and 2s to the end, resulting in [0, 0, 1, 1, 2].
🧠 Conceptual
expertWhy Dutch Flag Algorithm is Optimal for Sorting Colors
Why is the Dutch Flag algorithm considered optimal for sorting an array containing only 0s, 1s, and 2s?
Attempts:
2 left
💡 Hint
Think about how many times the array is traversed 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 very efficient.
