Challenge - 5 Problems
Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Trace the recursive calls and swaps carefully to see the order of permutations.
✗ Incorrect
The code generates permutations by swapping elements starting from the current index. The order of permutations is lexicographic based on the swapping sequence.
🧠 Conceptual
intermediate1:00remaining
Number of Permutations for an Array
If an array has 5 distinct elements, how many unique permutations can be generated?
Attempts:
2 left
💡 Hint
Recall the factorial function for counting permutations.
✗ Incorrect
The number of permutations of n distinct elements is n! (n factorial). For 5 elements, 5! = 120.
🔧 Debug
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the loop boundary in the base case printing loop.
✗ Incorrect
The base case loop uses 'i < end' instead of 'i <= end', so it misses printing the last element of the array.
📝 Syntax
advanced1: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]); } } }
Attempts:
2 left
💡 Hint
Look carefully at the end of each statement inside the for loop.
✗ Incorrect
The line 'swap(&arr[start], &arr[i])' is missing a semicolon, causing a syntax error.
🚀 Application
expert3:00remaining
Count Unique Permutations with Duplicate Elements
Given the array [1, 2, 2, 3], how many unique permutations can be generated?
Attempts:
2 left
💡 Hint
Use the formula for permutations of multiset: n! / (n1! * n2! * ...)
✗ Incorrect
The array has 4 elements with '2' repeated twice. Number of unique permutations = 4! / 2! = 24 / 2 = 12.