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#
int[] arr = {2, 0, 2, 1, 1, 0}; int pivot = 1; int low = 0, mid = 0, high = arr.Length - 1; while (mid <= high) { if (arr[mid] < pivot) { int temp = arr[low]; arr[low] = arr[mid]; arr[mid] = temp; low++; mid++; } else if (arr[mid] == pivot) { mid++; } else { int temp = arr[mid]; arr[mid] = arr[high]; arr[high] = temp; high--; } } Console.WriteLine(string.Join(", ", arr));
Attempts:
2 left
💡 Hint
Remember the algorithm moves elements less than pivot to the front, equal stay in 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 processing, the array becomes sorted as 0, 0, 1, 1, 2, 2.
🧠 Conceptual
intermediateNumber of swaps in Dutch National Flag algorithm
Given an array of size n with only three distinct values, what is the maximum number of swaps the Dutch National Flag algorithm performs in the worst case?
Attempts:
2 left
💡 Hint
Each element is swapped at most once or twice during partitioning.
✗ Incorrect
The algorithm processes each element at most once, and each swap moves an element closer to its final position. Hence, the maximum swaps are proportional to n.
🔧 Debug
advancedIdentify the bug in this Dutch National Flag implementation
What error does the following C# code produce when run on array {1, 0, 2} with pivot = 1?
DSA C#
int[] arr = {1, 0, 2}; int pivot = 1; int low = 0, mid = 0, high = arr.Length - 1; while (mid < high) { if (arr[mid] < pivot) { int temp = arr[low]; arr[low] = arr[mid]; arr[mid] = temp; low++; mid++; } else if (arr[mid] == pivot) { mid++; } else { int temp = arr[mid]; arr[mid] = arr[high]; arr[high] = temp; high--; } } Console.WriteLine(string.Join(", ", arr));
Attempts:
2 left
💡 Hint
Check the loop condition and how mid and high pointers move.
✗ Incorrect
The code runs correctly for this specific array {1, 0, 2}, outputting 0, 1, 2. However, the loop condition 'mid < high' can lead to incorrect partitioning in other cases (e.g., {2, 0, 1} outputs 1, 0, 2 instead of 0, 1, 2) because it skips processing when mid == high.
🚀 Application
advancedUsing Dutch National Flag to sort colors
You have an array representing colors coded as 0 (red), 1 (white), and 2 (blue). Which code snippet correctly sorts the array in one pass using Dutch National Flag algorithm?
Attempts:
2 left
💡 Hint
Dutch National Flag uses three pointers and swaps based on comparison with pivot.
✗ Incorrect
Option D implements the classic Dutch National Flag algorithm with three pointers and swaps in one pass. Other options are either sorting by built-in or inefficient methods or incomplete logic.
🧠 Conceptual
expertWhy Dutch National Flag algorithm is O(n) and not O(n^2)?
Why does the Dutch National Flag algorithm run in linear time O(n) instead of quadratic O(n^2) despite nested swaps?
Attempts:
2 left
💡 Hint
Think about how many times each element is processed and how pointers move.
✗ Incorrect
The algorithm uses three pointers that move through the array once. Each swap moves an element closer to its final position, so no element is processed multiple times, resulting in O(n) time.
