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 Mixed Array
What is the output of the following Java code that partitions an array using the Dutch National Flag algorithm with pivot value 1?
DSA Java
public class DNFFunction { public static void dutchNationalFlag(int[] arr, int pivot) { 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--; } } } public static void main(String[] args) { int[] arr = {2, 0, 2, 1, 1, 0}; dutchNationalFlag(arr, 1); for (int num : arr) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Focus on how the pointers low, mid, and high move and swap elements based on comparison with pivot.
✗ Incorrect
The algorithm partitions the array into three parts: less than pivot (0), equal to pivot (1), and greater than pivot (2). After processing, the array is 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
In the worst case, each element can be swapped at most once, so the maximum number of swaps is proportional to n.
🔧 Debug
advancedIdentify the bug in this Dutch National Flag implementation
What error will this Java code produce when run?
DSA Java
public class DNFBug { public static void dnf(int[] arr, int pivot) { 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--; } } } public static void main(String[] args) { int[] arr = {2, 0, 1}; dnf(arr, 1); for (int num : arr) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Check the loop condition and how pointers move inside the loop.
✗ Incorrect
The loop condition 'mid < high' causes the loop to miss processing when mid equals high, leading to an infinite loop in some cases.
📝 Syntax
advancedCorrect syntax for swapping elements in Java array
Which option correctly swaps elements arr[i] and arr[j] in Java?
Attempts:
2 left
💡 Hint
Java does not support tuple unpacking or direct swap function for primitives.
✗ Incorrect
Option A uses a temporary variable to swap values correctly. Option A swaps but assigns incorrectly, causing wrong swap.
🚀 Application
expertResulting array after multiple Dutch National Flag partitions
Given the array [3, 2, 1, 2, 3, 1, 2] and applying the Dutch National Flag partition twice: first with pivot=2, then with pivot=3, what is the final array?
DSA Java
public class DNFDualPartition { public static void dnf(int[] arr, int pivot) { 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--; } } } public static void main(String[] args) { int[] arr = {3, 2, 1, 2, 3, 1, 2}; dnf(arr, 2); dnf(arr, 3); for (int num : arr) { System.out.print(num + " "); } } }
Attempts:
2 left
💡 Hint
Apply the partition step by step and track the array after each partition.
✗ Incorrect
First partition with pivot=2 sorts elements into <2, ==2, >2. Then partition with pivot=3 sorts the array further, resulting in sorted order 1 1 2 2 2 3 3.
