Challenge - 5 Problems
Permutation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Permutation Generator
What is the output of the following JavaScript code that generates all permutations of the array [1, 2]?
DSA Javascript
function permute(arr) {
if (arr.length === 0) return [[]];
const first = arr[0];
const rest = permute(arr.slice(1));
const result = [];
for (const perm of rest) {
for (let i = 0; i <= perm.length; i++) {
const newPerm = perm.slice(0, i).concat([first], perm.slice(i));
result.push(newPerm);
}
}
return result;
}
console.log(permute([1, 2]));Attempts:
2 left
💡 Hint
Think about how the function inserts the first element into every possible position of smaller permutations.
✗ Incorrect
The function recursively generates permutations by inserting the first element into every position of permutations of the rest of the array. For [1,2], it produces [[1,2],[2,1]].
❓ Predict Output
intermediateOutput of Permutation Using Heap's Algorithm
What is the output of this JavaScript code using Heap's algorithm to generate permutations of [1, 2, 3]?
DSA Javascript
function heapPermute(n, arr, result = []) {
if (n === 1) {
result.push(arr.slice());
} else {
for (let i = 0; i < n - 1; i++) {
heapPermute(n - 1, arr, result);
if (n % 2 === 0) {
[arr[i], arr[n - 1]] = [arr[n - 1], arr[i]];
} else {
[arr[0], arr[n - 1]] = [arr[n - 1], arr[0]];
}
}
heapPermute(n - 1, arr, result);
}
return result;
}
console.log(heapPermute(3, [1, 2, 3]));Attempts:
2 left
💡 Hint
Heap's algorithm generates permutations by swapping elements based on parity of n.
✗ Incorrect
Heap's algorithm produces permutations in the order: [1,2,3], [2,1,3], [3,1,2], [1,3,2], [2,3,1], [3,2,1].
🔧 Debug
advancedIdentify the Error in Permutation Code
What error will this JavaScript code produce when generating permutations of [1, 2, 3]?
DSA Javascript
function permute(arr) {
if (arr.length === 0) return [];
const first = arr[0];
const rest = permute(arr.slice(1));
const result = [];
for (const perm of rest) {
for (let i = 0; i <= perm.length; i++) {
const newPerm = perm.slice(0, i).concat([first], perm.slice(i));
result.push(newPerm);
}
}
return result;
}
console.log(permute([1, 2, 3]));Attempts:
2 left
💡 Hint
Check the base case return value and how it affects recursion.
✗ Incorrect
The base case returns an empty array [], so the recursive call returns [] and the for loop over rest does not run, resulting in an empty array output.
🚀 Application
advancedNumber of Permutations for Array of Length 4
How many permutations will be generated for an array of length 4?
Attempts:
2 left
💡 Hint
Number of permutations of n distinct elements is n factorial (n!).
✗ Incorrect
4! = 4 × 3 × 2 × 1 = 24 permutations.
🧠 Conceptual
expertTime Complexity of Generating All Permutations
What is the time complexity of generating all permutations of an array of length n?
Attempts:
2 left
💡 Hint
Consider how many permutations exist and how much work is done per permutation.
✗ Incorrect
There are n! permutations and generating each takes at least constant time, so total time is O(n!).
