Bird
0
0
DSA Cprogramming~20 mins

Dutch National Flag Three Way Partition in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dutch National Flag Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Dutch National Flag Partition on Array
What is the output of the following code after performing the Dutch National Flag partition on the array?
DSA C
void dutchFlagPartition(int arr[], int size, int pivot) {
    int low = 0, mid = 0, high = size - 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--;
        }
    }
}

int main() {
    int arr[] = {2, 0, 2, 1, 1, 0};
    int size = 6;
    int pivot = 1;
    dutchFlagPartition(arr, size, pivot);
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
A1 1 0 0 2 2
B2 2 1 1 0 0
C0 1 0 1 2 2
D0 0 1 1 2 2
Attempts:
2 left
💡 Hint
Remember the algorithm moves elements less than pivot to the front, equal in the middle, and greater to the end.
🧠 Conceptual
intermediate
1:30remaining
Number 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 can perform in the worst case?
An
B2n
Cn/2
D3n
Attempts:
2 left
💡 Hint
The algorithm performs one swap for each element < pivot or > pivot.
🔧 Debug
advanced
2:00remaining
Identify the bug in Dutch National Flag implementation
What error does the following code produce when run on array {1, 2, 0} with pivot 1?
DSA C
void dutchFlagPartition(int arr[], int size, int pivot) {
    int low = 0, mid = 0, high = size - 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--;
        }
    }
}

int main() {
    int arr[] = {1, 2, 0};
    int size = 3;
    int pivot = 1;
    dutchFlagPartition(arr, size, pivot);
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    return 0;
}
AArray index out of bounds
BIncorrect output
CCorrect output: 0 1 2
DSegmentation fault
Attempts:
2 left
💡 Hint
Check the loop condition carefully and how mid and high pointers move.
📝 Syntax
advanced
1:00remaining
Syntax error in Dutch National Flag code snippet
Which option contains a syntax error in the Dutch National Flag partition implementation?
DSA C
void dutchFlagPartition(int arr[], int size, int pivot) {
    int low = 0, mid = 0, high = size - 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--;
        }
    }
}
ANo syntax error
BMissing closing brace for while loop
CMissing type declaration for temp variable
DMissing semicolon after high--
Attempts:
2 left
💡 Hint
Check all braces and semicolons carefully.
🚀 Application
expert
2:30remaining
Using Dutch National Flag for Sorting Colors
Given an array representing colors as integers 0 (red), 1 (white), and 2 (blue), which option correctly sorts the array using the Dutch National 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--;
        }
    }
}
ASorts array but only works if array is already partially sorted
BSorts array but uses extra O(n) space for temporary array
CSorts array in one pass with O(n) time and O(1) space
DSorts array in O(n^2) time due to nested loops
Attempts:
2 left
💡 Hint
Dutch National Flag algorithm is known for linear time and constant space sorting of three distinct values.