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 Function for [1, 2]
What is the output of the following TypeScript code that generates all permutations of the array [1, 2]?
DSA Typescript
function permute(nums: number[]): number[][] {
const result: number[][] = [];
function backtrack(start: number) {
if (start === nums.length) {
result.push([...nums]);
return;
}
for (let i = start; i < nums.length; i++) {
[nums[start], nums[i]] = [nums[i], nums[start]];
backtrack(start + 1);
[nums[start], nums[i]] = [nums[i], nums[start]];
}
}
backtrack(0);
return result;
}
console.log(permute([1, 2]));Attempts:
2 left
💡 Hint
Think about all ways to arrange two numbers.
✗ Incorrect
The function generates all permutations by swapping elements. For [1, 2], the permutations are [1, 2] and [2, 1].
❓ Predict Output
intermediate2:00remaining
Number of Permutations for Array [1, 2, 3]
What is the number of permutations generated by the following code for the array [1, 2, 3]?
DSA Typescript
function permute(nums: number[]): number[][] {
const result: number[][] = [];
function backtrack(start: number) {
if (start === nums.length) {
result.push([...nums]);
return;
}
for (let i = start; i < nums.length; i++) {
[nums[start], nums[i]] = [nums[i], nums[start]];
backtrack(start + 1);
[nums[start], nums[i]] = [nums[i], nums[start]];
}
}
backtrack(0);
return result;
}
const permutations = permute([1, 2, 3]);
console.log(permutations.length);Attempts:
2 left
💡 Hint
Number of permutations of n distinct elements is n factorial.
✗ Incorrect
For 3 elements, the number of permutations is 3! = 6.
🔧 Debug
advanced2:00remaining
Identify the Error in Permutation Code
What error will this TypeScript code produce when trying to generate permutations of [1, 2, 3]?
DSA Typescript
function permute(nums: number[]): number[][] {
const result: number[][] = [];
function backtrack(start: number) {
if (start === nums.length) {
result.push(nums);
return;
}
for (let i = start; i < nums.length; i++) {
[nums[start], nums[i]] = [nums[i], nums[start]];
backtrack(start + 1);
[nums[start], nums[i]] = [nums[i], nums[start]];
}
}
backtrack(0);
return result;
}
console.log(permute([1, 2, 3]));Attempts:
2 left
💡 Hint
Check how arrays are added to the result.
✗ Incorrect
The code pushes the original nums array reference multiple times without copying it, so all entries in result point to the same array, which ends up as the last permutation.
🧠 Conceptual
advanced2:00remaining
Understanding Backtracking in Permutations
In the backtracking approach to generate permutations, why do we swap elements back after the recursive call?
Attempts:
2 left
💡 Hint
Think about how the array changes during recursion.
✗ Incorrect
Swapping back restores the array to its previous state so the next iterations can generate new permutations without interference.
🚀 Application
expert3:00remaining
Permutations with Duplicate Elements
Given the array [1, 1, 2], which option correctly outputs all unique permutations using backtracking?
DSA Typescript
function permuteUnique(nums: number[]): number[][] {
nums.sort((a, b) => a - b);
const result: number[][] = [];
const used = new Array(nums.length).fill(false);
function backtrack(path: number[]) {
if (path.length === nums.length) {
result.push([...path]);
return;
}
for (let i = 0; i < nums.length; i++) {
if (used[i]) continue;
if (i > 0 && nums[i] === nums[i - 1] && !used[i - 1]) continue;
used[i] = true;
path.push(nums[i]);
backtrack(path);
path.pop();
used[i] = false;
}
}
backtrack([]);
return result;
}
console.log(permuteUnique([1, 1, 2]));Attempts:
2 left
💡 Hint
Duplicates are skipped by checking previous usage.
✗ Incorrect
The code sorts the array and skips duplicates by checking if the previous identical element was used, ensuring unique permutations only.