Bird
Raised Fist0
DSA Cprogramming~20 mins

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

Choose your learning style10 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! * ...)

Practice

(1/5)
1. What does the process of generating all permutations of an array mean?
easy
A. Finding the maximum element in the array
B. Sorting the array in ascending order
C. Listing all possible orders of the array elements
D. Removing duplicate elements from the array

Solution

  1. Step 1: Understand permutations

    Permutations are all possible ways to arrange elements in order.
  2. Step 2: Apply to array elements

    Generating all permutations means listing every possible order of the array elements.
  3. Final Answer:

    Listing all possible orders of the array elements -> Option C
  4. Quick Check:

    Permutations = all orders [OK]
Hint: Permutations = all ways to arrange elements [OK]
Common Mistakes:
  • Confusing permutations with sorting
  • Thinking permutations remove duplicates
  • Mixing permutations with finding max/min
2. Which of the following is the correct base case for a recursive function generating all permutations of an array arr of size n?
easy
A. if (start > n) { print(arr); return; }
B. if (start == n) { print(arr); return; }
C. if (n == 0) { return; }
D. if (start == 0) { print(arr); return; }

Solution

  1. Step 1: Understand recursion base case

    The base case occurs when the start index reaches the array size, meaning a full permutation is formed.
  2. Step 2: Match base case condition

    Condition start == n means all positions are fixed, so print the permutation and return.
  3. Final Answer:

    if (start == n) { print(arr); return; } -> Option B
  4. Quick Check:

    Base case when start equals array size [OK]
Hint: Base case: start index equals array length [OK]
Common Mistakes:
  • Using start > n instead of start == n
  • Printing when start == 0 (start of recursion)
  • Missing base case leading to infinite recursion
3. What is the output of the following C code snippet?
void permute(int *arr, int start, int n) {
    if (start == n) {
        for (int i = 0; i < n; i++) printf("%d ", arr[i]);
        printf("\n");
        return;
    }
    for (int i = start; i < n; i++) {
        int temp = arr[start];
        arr[start] = arr[i];
        arr[i] = temp;
        permute(arr, start + 1, n);
        temp = arr[start];
        arr[start] = arr[i];
        arr[i] = temp;
    }
}

int main() {
    int arr[] = {1, 2};
    permute(arr, 0, 2);
    return 0;
}
medium
A. 1 2 \n 2 1 \n
B. 1 2 \n 1 2 \n
C. 2 1 \n 1 2 \n
D. No output

Solution

  1. Step 1: Trace first call permute(arr, 0, 2)

    Start=0, loop i=0 to 1. Swap arr[0] with arr[0], call permute(arr,1,2).
  2. Step 2: Trace second call permute(arr, 1, 2)

    Start=1, loop i=1 to 1. Swap arr[1] with arr[1], call permute(arr,2,2). Base case reached, print "1 2".
  3. Step 3: Backtrack and swap arr[0] with arr[1]

    Now arr = {2,1}. Call permute(arr,1,2) again, print "2 1".
  4. Final Answer:

    1 2 2 1 -> Option A
  5. Quick Check:

    All permutations of [1,2] printed [OK]
Hint: Swap and backtrack to generate permutations [OK]
Common Mistakes:
  • Not swapping back after recursive call
  • Printing incomplete permutations
  • Confusing loop indices
4. Identify the error in the following code snippet that generates permutations of an array arr of size n:
void permute(int *arr, int start, int n) {
    if (start == n) {
        for (int i = 0; i < n; i++) printf("%d ", arr[i]);
        printf("\n");
        return;
    }
    for (int i = start; i < n; i++) {
        int temp = arr[start];
        arr[start] = arr[i];
        arr[i] = temp;
        permute(arr, start + 1, n);
        // Missing swap back here
    }
}
medium
A. Missing swap back after recursive call causes incorrect permutations
B. Base case condition should be start > n
C. Loop should run from i = 0 to n
D. Printing inside base case is incorrect

Solution

  1. Step 1: Understand swap and backtrack

    After swapping and recursive call, we must swap back to restore original array for next iteration.
  2. Step 2: Identify missing swap back

    The comment shows swap back is missing, so array stays changed, causing wrong permutations.
  3. Final Answer:

    Missing swap back after recursive call causes incorrect permutations -> Option A
  4. Quick Check:

    Swap back needed to restore array [OK]
Hint: Always swap back after recursion to restore array [OK]
Common Mistakes:
  • Forgetting to swap back after recursion
  • Changing loop range incorrectly
  • Wrong base case condition
5. Given an array arr = {1, 2, 2}, how many unique permutations will a correct algorithm generate that avoids duplicates?
hard
A. 6
B. 2
C. 4
D. 3

Solution

  1. Step 1: Calculate total permutations without duplicates

    For 3 elements with two '2's, total permutations = 3! / 2! = 3.
  2. Step 2: List unique permutations

    Unique permutations are: [1,2,2], [2,1,2], [2,2,1]. But careful with counting duplicates in generation.
  3. Step 3: Consider correct algorithm avoiding duplicates

    Some algorithms generate 4 permutations including one duplicate if not careful, but correct unique count is 3.
  4. Final Answer:

    3 -> Option D
  5. Quick Check:

    Unique permutations = 3 [OK]
Hint: Use factorial formula with duplicates: n!/k! [OK]
Common Mistakes:
  • Counting duplicates as unique permutations
  • Ignoring duplicate elements in calculation
  • Assuming all permutations are unique
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard