0
0
DSA Cprogramming~20 mins

Generate All Permutations of Array in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Permutations for a 3-Element Array
What is the output of the following C code that generates all permutations of the array [1, 2, 3]?
DSA C
#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void permute(int *arr, int start, int end) {
    if (start == end) {
        for (int i = 0; i <= end; i++) {
            printf("%d ", arr[i]);
        }
        printf("\n");
    } else {
        for (int i = start; i <= end; i++) {
            swap(&arr[start], &arr[i]);
            permute(arr, start + 1, end);
            swap(&arr[start], &arr[i]);
        }
    }
}

int main() {
    int arr[] = {1, 2, 3};
    permute(arr, 0, 2);
    return 0;
}
A
1 2 3 
1 3 2 
2 1 3 
2 3 1 
3 2 1 
3 1 2 
B
1 2 3 
2 1 3 
1 3 2 
3 1 2 
2 3 1 
3 2 1 
C
1 2 3 
1 3 2 
2 1 3 
2 3 1 
3 1 2 
3 2 1 
D
1 2 3 
1 3 2 
3 2 1 
2 1 3 
2 3 1 
3 1 2 
Attempts:
2 left
💡 Hint
Trace the recursive calls and swaps carefully to see the order of permutations.
🧠 Conceptual
intermediate
1:00remaining
Number of Permutations for an Array
If an array has 5 distinct elements, how many unique permutations can be generated?
A60
B25
C10
D120
Attempts:
2 left
💡 Hint
Recall the factorial function for counting permutations.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Permutation Code
What error will this code produce when generating permutations of [1, 2, 3]?
DSA C
#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void permute(int *arr, int start, int end) {
    if (start == end) {
        for (int i = 0; i < end; i++) {
            printf("%d ", arr[i]);
        }
        printf("\n");
    } else {
        for (int i = start; i <= end; i++) {
            swap(&arr[start], &arr[i]);
            permute(arr, start + 1, end);
            swap(&arr[start], &arr[i]);
        }
    }
}

int main() {
    int arr[] = {1, 2, 3};
    permute(arr, 0, 2);
    return 0;
}
APrints incomplete permutations missing the last element
BSegmentation fault due to invalid memory access
CInfinite recursion causing stack overflow
DNo output printed
Attempts:
2 left
💡 Hint
Check the loop boundary in the base case printing loop.
📝 Syntax
advanced
1:30remaining
Identify Syntax Error in Permutation Code
Which option contains a syntax error in the permutation function?
DSA C
void permute(int *arr, int start, int end) {
    if (start == end) {
        for (int i = 0; i <= end; i++) {
            printf("%d ", arr[i]);
        }
        printf("\n");
    } else {
        for (int i = start; i <= end; i++) {
            swap(&arr[start], &arr[i]);
            permute(arr, start + 1, end);
            swap(&arr[start], &arr[i]);
        }
    }
}
AIncorrect printf format specifier
BMissing semicolon after swap(&arr[start], &arr[i])
CMissing braces around for loop
DIncorrect function return type
Attempts:
2 left
💡 Hint
Look carefully at the end of each statement inside the for loop.
🚀 Application
expert
3:00remaining
Count Unique Permutations with Duplicate Elements
Given the array [1, 2, 2, 3], how many unique permutations can be generated?
A12
B24
C6
D18
Attempts:
2 left
💡 Hint
Use the formula for permutations of multiset: n! / (n1! * n2! * ...)