0
0
DSA Typescriptprogramming~10 mins

Generate All Subsets Powerset 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 initialize the result array for storing subsets.

DSA Typescript
function subsets(nums: number[]): number[][] {
  const result = [1];
  return result;
}
Drag options to blanks, or click blank then click option'
A{}
B[]
Cnull
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using {} which creates an object instead of an array.
Using null or 0 which are not arrays.
2fill in blank
medium

Complete the code to start the recursive backtracking call.

DSA Typescript
function subsets(nums: number[]): number[][] {
  const result: number[][] = [];
  function backtrack(start: number, path: number[]) {
    result.push([...path]);
    for (let i = start; i < nums.length; i++) {
      path.push(nums[i]);
      backtrack([1], path);
      path.pop();
    }
  }
  backtrack(0, []);
  return result;
}
Drag options to blanks, or click blank then click option'
Ai + 1
Bstart + 1
Ci
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Passing start + 1 which skips elements incorrectly.
Passing i which causes infinite recursion.
3fill in blank
hard

Fix the error in the base case to correctly add the current subset to the result.

DSA Typescript
function subsets(nums: number[]): number[][] {
  const result: number[][] = [];
  function backtrack(start: number, path: number[]) {
    [1];
    for (let i = start; i < nums.length; i++) {
      path.push(nums[i]);
      backtrack(i + 1, path);
      path.pop();
    }
  }
  backtrack(0, []);
  return result;
}
Drag options to blanks, or click blank then click option'
Aresult.push(path.slice())
Bresult.push(path)
Cresult.push([...path])
Dresult.push(path.pop())
Attempts:
3 left
💡 Hint
Common Mistakes
Pushing path directly causes all entries to reference the same array.
Using path.pop() removes elements and returns a number, not the subset.
4fill in blank
hard

Fill both blanks to complete the for loop that generates subsets.

DSA Typescript
function subsets(nums: number[]): number[][] {
  const result: number[][] = [];
  function backtrack([1]: number, path: number[]) {
    result.push([...path]);
    for (let i = [2]; i < nums.length; i++) {
      path.push(nums[i]);
      backtrack(i + 1, path);
      path.pop();
    }
  }
  backtrack(0, []);
  return result;
}
Drag options to blanks, or click blank then click option'
Astart
Bindex
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causing confusion.
Starting loop at 0 inside backtrack causes duplicates.
5fill in blank
hard

Fill all three blanks to complete the subsets function with backtracking.

DSA Typescript
function subsets(nums: number[]): number[][] {
  const result: number[][] = [];
  function backtrack([1]: number, [2]: number[]) {
    result.push([...[3]]);
    for (let i = start; i < nums.length; i++) {
      path.push(nums[i]);
      backtrack(i + 1, path);
      path.pop();
    }
  }
  backtrack(0, []);
  return result;
}
Drag options to blanks, or click blank then click option'
Astart
Bpath
Dnums
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names causing reference errors.
Pushing the wrong variable to result.