Challenge - 5 Problems
Permutation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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#
using System; using System.Collections.Generic; class Program { static void Main() { int[] arr = {1, 2, 3}; var result = new List<List<int>>(); Permute(arr, 0, result); foreach(var perm in result) { Console.WriteLine(string.Join(",", perm)); } } static void Permute(int[] arr, int start, List<List<int>> result) { if (start == arr.Length) { result.Add(new List<int>(arr)); return; } for (int i = start; i < arr.Length; i++) { Swap(arr, start, i); Permute(arr, start + 1, result); Swap(arr, start, i); } } static void Swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }
Attempts:
2 left
💡 Hint
Trace the recursive calls and swaps carefully to see the order of permutations generated.
✗ Incorrect
The code uses backtracking with swapping to generate permutations. The order is lexicographic based on the swapping sequence, producing permutations in the order: [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1].
🧠 Conceptual
intermediateNumber of Permutations for an Array of Length N
If you have an array of length N, how many unique permutations does it have?
Attempts:
2 left
💡 Hint
Think about how many choices you have for each position in the permutation.
✗ Incorrect
The number of unique permutations of an array of length N is N factorial (N!). This is because for the first position you have N choices, for the second N-1, and so on, multiplying to N!.
🔧 Debug
advancedIdentify the Bug in Permutation Generation Code
What error will the following code produce when trying to generate permutations of [1, 2, 3]?
DSA C#
using System; using System.Collections.Generic; class Program { static void Main() { int[] arr = {1, 2, 3}; var result = new List<List<int>>(); Permute(arr, 0, result); foreach(var perm in result) { Console.WriteLine(string.Join(",", perm)); } } static void Permute(int[] arr, int start, List<List<int>> result) { if (start == arr.Length) { result.Add(new List<int>(arr)); return; } for (int i = start; i < arr.Length; i++) { Swap(arr, start, i); Permute(arr, start + 1, result); // Missing swap back here } } static void Swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }
Attempts:
2 left
💡 Hint
Check if the array is restored after recursive calls.
✗ Incorrect
The missing swap back after the recursive call causes the array to remain altered, so some permutations are skipped or incorrect. This leads to incomplete output, not an exception.
📝 Syntax
advancedIdentify Syntax Error in Permutation Code
Which option contains a syntax error in this C# permutation generation snippet?
DSA C#
static void Permute(int[] arr, int start, List<List<int>> result) { if (start == arr.Length) { result.Add(new List<int>(arr)); return; } for (int i = start; i < arr.Length; i++) { Swap(arr, start, i); Permute(arr, start + 1, result); Swap(arr, start, i); } }
Attempts:
2 left
💡 Hint
Look for missing semicolons or braces.
✗ Incorrect
Option B is missing a semicolon at the end of the Swap call, causing a syntax error.
🚀 Application
expertCount Permutations with a Fixed Element at First Position
Given an array of length N, how many permutations have the first element fixed as the original first element?
Attempts:
2 left
💡 Hint
Fixing one element reduces the permutation problem to the remaining elements.
✗ Incorrect
Fixing the first element means we only permute the remaining N-1 elements, so the count is (N-1)!.
