0
0
DSA Typescriptprogramming~10 mins

Generate All Permutations of Array in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start the permutation generation by calling the helper function.

DSA Typescript
function permute(nums: number[]): number[][] {
  const result: number[][] = [];
  function backtrack(start: number) {
    // helper function body
  }
  [1];
  return result;
}
Drag options to blanks, or click blank then click option'
Abacktrack(0)
Bbacktrack(nums.length)
Cbacktrack(-1)
Dbacktrack()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling backtrack with the length of the array instead of 0.
Calling backtrack without any argument.
Starting from a negative index.
2fill in blank
medium

Complete the code to swap elements at two indices in the array.

DSA Typescript
function swap(nums: number[], i: number, j: number): void {
  const temp = nums[i];
  nums[i] = nums[[1]];
  nums[[1]] = temp;
}
Drag options to blanks, or click blank then click option'
A0
Bj
Cnums.length - 1
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j for the second index.
Using a fixed index like 0 or nums.length - 1.
3fill in blank
hard

Fix the error in the base case condition to add a copy of the current permutation to the result.

DSA Typescript
function permute(nums: number[]): number[][] {
  const result: number[][] = [];
  function backtrack(start: number) {
    if (start [1] nums.length) {
      result.push([...nums]);
      return;
    }
    // rest of code
  }
  backtrack(0);
  return result;
}
Drag options to blanks, or click blank then click option'
A>
B<
C===
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than or greater than instead of equality.
Using less than or equal which triggers too early.
4fill in blank
hard

Fill both blanks to complete the for loop and swap calls inside the backtrack function.

DSA Typescript
function backtrack(start: number) {
  if (start === nums.length) {
    result.push([...nums]);
    return;
  }
  for (let i = [1]; i < nums.length; i++) {
    swap(nums, start, i);
    backtrack([2]);
    swap(nums, start, i);
  }
}
Drag options to blanks, or click blank then click option'
Astart
Bi
Cstart + 1
Di + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 0 instead of start.
Calling backtrack with i instead of start + 1.
5fill in blank
hard

Fill all three blanks to complete the full permutation function with swap helper.

DSA Typescript
function permute(nums: number[]): number[][] {
  const result: number[][] = [];
  function swap(arr: number[], i: number, j: number): void {
    const temp = arr[i];
    arr[i] = arr[[1]];
    arr[[2]] = temp;
  }
  function backtrack(start: number) {
    if (start === nums.length) {
      result.push([...nums]);
      return;
    }
    for (let i = [3]; i < nums.length; i++) {
      swap(nums, start, i);
      backtrack(start + 1);
      swap(nums, start, i);
    }
  }
  backtrack(0);
  return result;
}
Drag options to blanks, or click blank then click option'
Aj
Bi
Cstart
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up i and j in swap assignments.
Starting loop from 0 instead of start.