Bird
Raised Fist0
DSA C++programming~10 mins

Generate All Permutations of Array in DSA C++ - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to swap two elements in the array.

DSA C++
void swap(int &a, int &b) {
    int temp = a;
    a = [1];
    b = temp;
}
Drag options to blanks, or click blank then click option'
A0
Ba
Cb
Dtemp
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning 'a' to 'temp' again instead of 'b'.
Forgetting to use reference parameters.
2fill in blank
medium

Complete the code to generate permutations by swapping elements.

DSA C++
void permute(int arr[], int l, int r) {
    if (l == r) {
        // print permutation
        for (int i = 0; i <= r; i++)
            std::cout << arr[i] << " ";
        std::cout << std::endl;
    } else {
        for (int i = l; i <= r; i++) {
            swap(arr[l], arr[[1]]);
            permute(arr, l + 1, r);
            swap(arr[l], arr[i]);
        }
    }
}
Drag options to blanks, or click blank then click option'
Ai
Bl
Cr
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with 'l' instead of 'i' causing no change.
Using 'r' which is the end index, not the loop index.
3fill in blank
hard

Fix the error in the base case condition to correctly detect when to print a permutation.

DSA C++
void permute(int arr[], int l, int r) {
    if (l [1] r) {
        for (int i = 0; i <= r; i++)
            std::cout << arr[i] << " ";
        std::cout << std::endl;
    } else {
        for (int i = l; i <= r; i++) {
            swap(arr[l], arr[i]);
            permute(arr, l + 1, r);
            swap(arr[l], arr[i]);
        }
    }
}
Drag options to blanks, or click blank then click option'
A==
B<
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' which causes printing at wrong times.
Using '<' or '>' which never triggers the base case properly.
4fill in blank
hard

Fill both blanks to create a function that prints all permutations of an array.

DSA C++
void printPermutations(int arr[], int n) {
    permute(arr, [1], [2]);
}
Drag options to blanks, or click blank then click option'
A0
Bn
Cn - 1
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'n' as last index causes out-of-bounds errors.
Starting from 1 misses the first element.
5fill in blank
hard

Fill all three blanks to complete the main function that calls permutation printing.

DSA C++
#include <iostream>

int main() {
    int arr[] = {1, 2, 3};
    int n = sizeof(arr) / sizeof([1]);
    printPermutations(arr, [2]);
    return [3];
}
Drag options to blanks, or click blank then click option'
Aarr[0]
Bn
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using '1' instead of '0' in sizeof calculation.
Returning 1 instead of 0 in main.

Practice

(1/5)
1. What does the process of generating all permutations of an array mean?
easy
A. Listing all possible orders of the array elements
B. Sorting the array in ascending order
C. Finding the maximum element in the array
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 A
  4. Quick Check:

    Permutations = all orders [OK]
Hint: Permutations = all possible orders of elements [OK]
Common Mistakes:
  • Confusing permutations with sorting
  • Thinking permutations remove duplicates
  • Assuming permutations find max or min
2. Which of the following C++ function signatures is correct for a recursive function generating all permutations of an integer array arr with size n</> and current index start?
easy
A. void permute(int arr[], int start, int n)
B. int permute(int arr[], int n, int start)
C. void permute(int arr[], int n)
D. int permute(int arr[], int start)

Solution

  1. Step 1: Check parameter order and types

    The function needs the array, current index (start), and total size (n) to generate permutations recursively.
  2. Step 2: Validate return type and parameters

    It should return void (no value) and have parameters (int arr[], int start, int n) in that order.
  3. Final Answer:

    void permute(int arr[], int start, int n) -> Option A
  4. Quick Check:

    Correct signature = void permute(arr, start, n) [OK]
Hint: Recursive permute needs array, start index, and size [OK]
Common Mistakes:
  • Wrong parameter order
  • Incorrect return type
  • Missing current index parameter
3. What will be 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++) std::cout << arr[i] << ' ';
    std::cout << '\n';
    return;
  }
  for (int i = start; i < n; i++) {
    std::swap(arr[start], arr[i]);
    permute(arr, start + 1, n);
    std::swap(arr[start], arr[i]);
  }
}

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

Solution

  1. Step 1: Trace first call with arr = {1, 2}

    Start = 0, n = 2. Swap arr[0] with arr[0] (no change), call permute with start=1.
  2. Step 2: Trace second call with start=1

    Swap arr[1] with arr[1] (no change), call permute with start=2 which prints "1 2".
  3. Step 3: Backtrack and swap arr[0] with arr[1]

    Array becomes {2, 1}, call permute with start=1 again.
  4. Step 4: At start=1 again, swap arr[1] with arr[1], call permute with start=2 prints "2 1".

  5. Final Answer:

    1 2 \n 2 1 \n -> Option B
  6. Quick Check:

    All permutations of {1,2} printed [OK]
Hint: Swap and backtrack to print all orders [OK]
Common Mistakes:
  • Not swapping back after recursion
  • Printing before base case
  • Incorrect loop range
4. Identify the error in the following code snippet for generating permutations of array arr of size n:
void permute(int arr[], int start, int n) {
  if (start == n) {
    for (int i = 0; i < n; i++) std::cout << arr[i] << ' ';
    std::cout << '\n';
    return;
  }
  for (int i = 0; i < n; i++) {
    std::swap(arr[start], arr[i]);
    permute(arr, start + 1, n);
    std::swap(arr[start], arr[i]);
  }
}
medium
A. Function should return int, not void
B. Base case condition should be start == n - 1
C. Swap should be done only once outside the loop
D. Loop should start from i = start, not 0

Solution

  1. Step 1: Analyze the for loop range

    The loop swaps arr[start] with arr[i]. If i starts at 0 always, it swaps elements already fixed in previous recursion steps.
  2. Step 2: Correct loop start index

    Loop should start from i = start to avoid repeating swaps and ensure correct permutations.
  3. Final Answer:

    Loop should start from i = start, not 0 -> Option D
  4. Quick Check:

    Loop from start to n fixes correct elements [OK]
Hint: Start loop from current index to avoid duplicate swaps [OK]
Common Mistakes:
  • Starting loop from 0 causes repeated permutations
  • Changing base case incorrectly
  • Misplacing swap calls
5. Given an array arr = {1, 2, 2}, how many unique permutations will a correct C++ program generate if it avoids duplicates?
hard
A. 2
B. 6
C. 3
D. 4

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

    They are {1,2,2}, {2,1,2}, {2,2,1} -- total 3 unique permutations.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    3! / 2! = 3 unique permutations [OK]
Hint: Divide factorial by duplicates factorial for unique count [OK]
Common Mistakes:
  • Counting duplicates as unique permutations
  • Using 3! = 6 without division
  • Ignoring repeated elements
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