Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Generate All Subsets Powerset in DSA Typescript - Interactive Practice

Choose your learning style10 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.

Practice

(1/5)
1. What does the term powerset mean in the context of sets?
easy
A. All possible subsets of a given set, including empty and full set
B. Only the subsets with exactly half the elements of the set
C. The set of elements that are not in the original set
D. The set containing only the largest element of the original set

Solution

  1. Step 1: Understand the definition of powerset

    The powerset of a set is the collection of all subsets, including the empty set and the set itself.
  2. Step 2: Compare options with definition

    All possible subsets of a given set, including empty and full set correctly describes all subsets, while others describe partial or unrelated sets.
  3. Final Answer:

    All possible subsets of a given set, including empty and full set -> Option A
  4. Quick Check:

    powerset = all subsets [OK]
Hint: Remember powerset means every subset possible [OK]
Common Mistakes:
  • Thinking powerset excludes empty set
  • Confusing powerset with complement set
  • Assuming powerset only has subsets of certain size
2. Which of the following TypeScript function signatures correctly represents a function that generates all subsets of an array of numbers?
easy
A. function generateSubsets(nums: number[][]): number[]
B. function generateSubsets(nums: number): number[]
C. function generateSubsets(nums: string[]): string
D. function generateSubsets(nums: number[]): number[][]

Solution

  1. Step 1: Analyze the input and output types

    The input is an array of numbers, so nums: number[] is correct. The output is all subsets, so an array of arrays of numbers number[][] is expected.
  2. Step 2: Match options with correct types

    function generateSubsets(nums: number[]): number[][] matches input and output types correctly. Others have mismatched types or wrong return types.
  3. Final Answer:

    function generateSubsets(nums: number[]): number[][] -> Option D
  4. Quick Check:

    Input array, output array of arrays [OK]
Hint: Input array, output array of arrays for subsets [OK]
Common Mistakes:
  • Returning a flat array instead of array of arrays
  • Using wrong input type like number instead of number[]
  • Returning a string instead of array
3. What is the output of the following TypeScript code?
function subsets(nums: number[]): number[][] {
  const result: number[][] = [[]];
  for (const num of nums) {
    const newSubsets = result.map(subset => [...subset, num]);
    result.push(...newSubsets);
  }
  return result;
}

console.log(subsets([1, 2]));
medium
A. [[], [1], [2], [1, 2]]
B. [[1], [2], [1, 2]]
C. [[], [1, 2]]
D. [[], [1], [2]]

Solution

  1. Step 1: Trace the initial state and loop

    Start with result = [[]]. For num=1, newSubsets = [[1]], result becomes [[], [1]]. For num=2, newSubsets = [[2], [1, 2]], result becomes [[], [1], [2], [1, 2]].
  2. Step 2: Confirm final output

    The function returns all subsets: empty, single elements, and full set.
  3. Final Answer:

    [[], [1], [2], [1, 2]] -> Option A
  4. Quick Check:

    All subsets of [1,2] listed [OK]
Hint: Each loop doubles subsets by adding current number [OK]
Common Mistakes:
  • Forgetting to include empty subset
  • Not spreading new subsets correctly
  • Returning partial subsets only
4. Identify the error in this TypeScript code that tries to generate all subsets:
function getSubsets(nums: number[]): number[][] {
  let result: number[][] = [];
  for (const num of nums) {
    const newSubsets = result.map(subset => [...subset, num]);
    result = [...result, ...newSubsets];
  }
  return result;
}

console.log(getSubsets([1, 2]));
medium
A. The spread operator is missing in the return statement
B. The map function is used incorrectly
C. The initial result should contain the empty subset [[]]
D. The function should return a flat array, not array of arrays

Solution

  1. Step 1: Check initial value of result

    The code starts with result = [], missing the empty subset []. This causes no subsets to be generated.
  2. Step 2: Understand impact on loop

    Since result is empty initially, result.map returns empty array each time, so no subsets are added.
  3. Final Answer:

    The initial result should contain the empty subset [[]] -> Option C
  4. Quick Check:

    Start with [[]] to build subsets [OK]
Hint: Always start subsets with empty set [[]] [OK]
Common Mistakes:
  • Starting with empty array instead of array with empty subset
  • Misusing map without initial subsets
  • Expecting output without base case
5. Given an array nums = [1, 2, 2], which of the following outputs correctly represents all unique subsets (powerset without duplicates) generated by a function that handles duplicates?
hard
A. [[], [1], [2], [2, 2]]
B. [[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]
C. [[], [1], [2], [1, 2], [1, 2, 2]]
D. [[], [1], [2], [1, 2], [2, 2], [1, 2, 2], [2, 1]]

Solution

  1. Step 1: Understand duplicates in input

    The input has duplicate '2's. The powerset must include subsets with one or two '2's but no repeated subsets.
  2. Step 2: List all unique subsets

    Subsets are: empty [], single elements [1], [2], pairs [1,2], [2,2], and triple [1,2,2]. [[], [1], [2], [1, 2], [2, 2], [1, 2, 2]] lists all correctly without duplicates.
  3. Final Answer:

    [[], [1], [2], [1, 2], [2, 2], [1, 2, 2]] -> Option B
  4. Quick Check:

    Include duplicates subsets once [OK]
Hint: Handle duplicates by sorting and skipping repeats [OK]
Common Mistakes:
  • Including duplicate subsets multiple times
  • Missing subsets with two duplicates
  • Adding permutations like [2,1] as separate subset
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard