0
0
DSA Typescriptprogramming~20 mins

Generate All Permutations of Array in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]));
A[[1,2],[2,1]]
B[[2,1],[1,2]]
C[[1,2]]
D[[2,1]]
Attempts:
2 left
💡 Hint
Think about all ways to arrange two numbers.
Predict Output
intermediate
2: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);
A3
B6
C9
D12
Attempts:
2 left
💡 Hint
Number of permutations of n distinct elements is n factorial.
🔧 Debug
advanced
2: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]));
AThe output contains multiple references to the same array, causing all permutations to be identical.
BSyntaxError due to missing semicolon.
CTypeError because result.push is called with a number instead of an array.
DRuntime error due to infinite recursion.
Attempts:
2 left
💡 Hint
Check how arrays are added to the result.
🧠 Conceptual
advanced
2:00remaining
Understanding Backtracking in Permutations
In the backtracking approach to generate permutations, why do we swap elements back after the recursive call?
ATo remove duplicate permutations from the result.
BTo sort the array after each permutation is generated.
CTo avoid infinite recursion by limiting the depth.
DTo restore the original array state so other permutations can be generated correctly.
Attempts:
2 left
💡 Hint
Think about how the array changes during recursion.
🚀 Application
expert
3: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]));
A[[1,1,2],[2,1,1]]
B[[1,1,2],[1,2,1],[2,1,1],[1,1,2]]
C[[1,1,2],[1,2,1],[2,1,1]]
D[[1,2,1],[2,1,1]]
Attempts:
2 left
💡 Hint
Duplicates are skipped by checking previous usage.