0
0
DSA Typescriptprogramming~20 mins

Generate All Subsets Powerset in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Powerset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of powerset generation with duplicates
What is the output of the following TypeScript code that generates all subsets (powerset) of an array with duplicates?
DSA Typescript
function subsets(nums: number[]): number[][] {
  const result: number[][] = [];
  nums.sort((a, b) => a - b);
  function backtrack(start: number, path: number[]) {
    result.push([...path]);
    for (let i = start; i < nums.length; i++) {
      if (i > start && nums[i] === nums[i - 1]) continue;
      path.push(nums[i]);
      backtrack(i + 1, path);
      path.pop();
    }
  }
  backtrack(0, []);
  return result;
}

console.log(subsets([1, 2, 2]));
A[[],[1],[2],[1,2],[2,2],[1,2,2]]
B[[],[1],[1,2],[1,2,2],[2],[2,2]]
C[[],[1],[2],[2,2],[1,2],[1,2,2]]
D[[],[1],[1,2],[2],[2,2],[1,2,2]]
Attempts:
2 left
💡 Hint
Duplicates are skipped to avoid repeated subsets. The subsets are generated in sorted order.
🧠 Conceptual
intermediate
1:00remaining
Number of subsets in powerset
Given an input array of length n, how many subsets does the powerset contain?
A2^n
Bn^2
Cn
Dn!
Attempts:
2 left
💡 Hint
Each element can either be included or excluded from a subset.
🔧 Debug
advanced
1:30remaining
Identify the bug in powerset generation
What error does the following TypeScript code produce when generating all subsets of [1,2]? 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(i + 1, path); path.pop(); } } backtrack(0, []); return result; } console.log(subsets([1, 2]));
AThe output contains repeated references to the same array, causing all subsets to be the same at the end.
BSyntaxError due to missing colon in function parameter.
CTypeError because path is undefined inside backtrack.
DThe output is correct with all subsets printed properly.
Attempts:
2 left
💡 Hint
Check how arrays are pushed into the result and if they are copied.
Predict Output
advanced
2:00remaining
Output of iterative powerset generation
What is the output of the following TypeScript code that generates all subsets iteratively for input [1, 2]?
DSA Typescript
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]));
A[[1],[2],[1,2],[]]
B[[],[1],[1,2],[2]]
C[[],[1],[2],[1,2]]
D[[],[2],[1],[1,2]]
Attempts:
2 left
💡 Hint
The code starts with empty subset and adds each number to existing subsets.
🧠 Conceptual
expert
1:30remaining
Time complexity of generating all subsets
What is the time complexity of generating all subsets (powerset) of an array of length n?
AO(n!)
BO(2^n)
CO(n^2)
DO(n * 2^n)
Attempts:
2 left
💡 Hint
Consider the number of subsets and the cost to copy each subset.