Bird
0
0
DSA Cprogramming~20 mins

Sort Colors Two Pointer Dutch Flag in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dutch Flag Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Dutch Flag Algorithm After One Pass
What is the state of the array after the first complete pass of the two-pointer Dutch Flag algorithm on the input array?
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--;
        }
    }
}

int main() {
    int nums[] = {2, 0, 2, 1, 1, 0};
    int size = 6;
    sortColors(nums, size);
    // Print array after sorting
    for (int i = 0; i < size; i++) {
        printf("%d ", nums[i]);
    }
    return 0;
}
A[2, 0, 0, 1, 1, 2]
B[0, 0, 1, 1, 2, 2]
C[0, 0, 2, 1, 1, 2]
D[0, 2, 2, 1, 1, 0]
Attempts:
2 left
💡 Hint
Trace the pointers low, mid, and high as the algorithm swaps elements.
🧠 Conceptual
intermediate
1:30remaining
Number of Swaps in Dutch Flag Algorithm
Given an array of n elements containing only 0s, 1s, and 2s, what is the maximum number of swaps the two-pointer Dutch Flag algorithm will perform in the worst case?
An^2
B2n
Cn/2
Dn
Attempts:
2 left
💡 Hint
Consider that each element is swapped at most once or twice.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Dutch Flag Implementation
What error will the following code produce when sorting colors using the Dutch 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--;
        }
    }
}
AArray not fully sorted because loop condition is incorrect
BSegmentation fault due to invalid memory access
CInfinite loop because mid is never incremented in some cases
DCompilation error due to missing semicolon
Attempts:
2 left
💡 Hint
Check the loop condition carefully and how it affects the pointers.
Predict Output
advanced
2:00remaining
Final Array State After Partial Dutch Flag Execution
What is the array state after executing the Dutch Flag algorithm up to the point where mid pointer reaches index 3 on the input array?
DSA C
int nums[] = {2, 0, 1, 2, 0, 1};
int low = 0, mid = 0, high = 5;
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--;
    }
    if (mid == 3) break;
}
A[0, 0, 1, 1, 2, 2]
B[2, 0, 1, 2, 0, 1]
C[0, 0, 1, 2, 2, 1]
D[0, 1, 0, 2, 2, 1]
Attempts:
2 left
💡 Hint
Simulate each step and watch how low, mid, and high pointers move.
🧠 Conceptual
expert
1:30remaining
Why Dutch Flag Algorithm is Optimal for Sorting Colors
Why is the two-pointer Dutch Flag algorithm considered optimal for sorting an array containing only 0s, 1s, and 2s?
AIt sorts the array in a single pass with constant extra space
BIt uses recursion to divide and conquer the array efficiently
CIt sorts the array by counting occurrences and then overwriting
DIt uses a built-in sort function optimized for small ranges
Attempts:
2 left
💡 Hint
Think about the number of passes and extra memory used.