Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Subsets Generation Using Bitmask in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Bitmask Subsets Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of subsets generated by bitmask
What is the output of the following TypeScript code that generates all subsets of [1, 2] using bitmask?
DSA Typescript
const nums = [1, 2];
const subsets: number[][] = [];
for (let mask = 0; mask < (1 << nums.length); mask++) {
  const subset: number[] = [];
  for (let i = 0; i < nums.length; i++) {
    if ((mask & (1 << i)) !== 0) {
      subset.push(nums[i]);
    }
  }
  subsets.push(subset);
}
console.log(subsets);
A[[], [1], [2], [1, 2]]
B[[1, 2], [1], [2], []]
C[[], [2], [1], [2, 1]]
D[[1], [], [2], [1, 2]]
Attempts:
2 left
💡 Hint
Think about how bitmask from 0 to 2^n - 1 represents subsets.
❓ Predict Output
intermediate
2:00remaining
Number of subsets generated by bitmask
How many subsets will be generated by the bitmask approach for an input array of length 4?
DSA Typescript
const nums = [10, 20, 30, 40];
const subsets: number[][] = [];
for (let mask = 0; mask < (1 << nums.length); mask++) {
  const subset: number[] = [];
  for (let i = 0; i < nums.length; i++) {
    if ((mask & (1 << i)) !== 0) {
      subset.push(nums[i]);
    }
  }
  subsets.push(subset);
}
console.log(subsets.length);
A8
B4
C16
D32
Attempts:
2 left
💡 Hint
Number of subsets of a set with n elements is 2^n.
🔧 Debug
advanced
2:00remaining
Identify the error in subset generation code
What error will the following TypeScript code produce when generating subsets using bitmask?
DSA Typescript
const nums = [5, 10, 15];
const subsets: number[][] = [];
for (let mask = 0; mask <= (1 << nums.length); mask++) {
  const subset: number[] = [];
  for (let i = 0; i < nums.length; i++) {
    if ((mask & (1 << i)) !== 0) {
      subset.push(nums[i]);
    }
  }
  subsets.push(subset);
}
console.log(subsets.length);
AThe code runs fine and outputs 8
BRuntime error: index out of bounds
CSyntaxError due to invalid bitwise operation
DOutputs 9 subsets, one extra invalid subset
Attempts:
2 left
💡 Hint
Check the loop condition for mask variable.
❓ Predict Output
advanced
2:00remaining
Output of subsets with filtering condition
What is the output of the following TypeScript code that generates subsets of [1, 2, 3] but only includes subsets with sum greater than 3?
DSA Typescript
const nums = [1, 2, 3];
const subsets: number[][] = [];
for (let mask = 0; mask < (1 << nums.length); mask++) {
  const subset: number[] = [];
  let sum = 0;
  for (let i = 0; i < nums.length; i++) {
    if ((mask & (1 << i)) !== 0) {
      subset.push(nums[i]);
      sum += nums[i];
    }
  }
  if (sum > 3) {
    subsets.push(subset);
  }
}
console.log(subsets);
A[[3], [1, 3], [2, 3], [1, 2, 3]]
B[[1, 3], [2, 3], [1, 2, 3]]
C[[2, 3], [1, 2, 3]]
D[[1, 2, 3]]
Attempts:
2 left
💡 Hint
Calculate sums of all subsets and check which are > 3.
🧠 Conceptual
expert
2:00remaining
Maximum subsets generated by bitmask for n elements
For an input array of length n, what is the maximum number of subsets generated using the bitmask approach?
A2^n
Bn^2
Cn!
Dn * 2
Attempts:
2 left
💡 Hint
Recall the total number of subsets of a set.

Practice

(1/5)
1. What does the bitmask technique help generate when applied to a set of elements?
easy
A. Only the largest subset
B. Only subsets with odd number of elements
C. Only subsets with even number of elements
D. All possible subsets of the set

Solution

  1. Step 1: Understand bitmask representation

    Each bit in the bitmask represents whether an element is included (1) or excluded (0) in a subset.
  2. Step 2: Generate subsets using bitmask

    By iterating through all bitmasks from 0 to 2^n - 1, we cover all combinations of elements, thus all subsets.
  3. Final Answer:

    All possible subsets of the set -> Option D
  4. Quick Check:

    Bitmask = All subsets [OK]
Hint: Bitmask from 0 to 2^n-1 covers all subsets [OK]
Common Mistakes:
  • Thinking bitmask generates only even or odd subsets
  • Confusing subsets with permutations
  • Assuming bitmask generates only largest subsets
2. Which of the following TypeScript code snippets correctly initializes the bitmask loop for generating subsets of an array arr of length n?
easy
A. for (let mask = 0; mask <= n; mask++) {}
B. for (let mask = 1; mask <= n; mask++) {}
C. for (let mask = 0; mask < (1 << n); mask++) {}
D. for (let mask = 0; mask < n; mask++) {}

Solution

  1. Step 1: Understand bitmask range

    Bitmask must cover all numbers from 0 to 2^n - 1, which is (1 << n) in code.
  2. Step 2: Check loop correctness

    Loop from 0 to less than (1 << n) correctly iterates all bitmasks representing subsets.
  3. Final Answer:

    for (let mask = 0; mask < (1 << n); mask++) {} -> Option C
  4. Quick Check:

    Loop 0 to 2^n-1 = for (let mask = 0; mask < (1 << n); mask++) {} [OK]
Hint: Use (1 << n) for total subsets count [OK]
Common Mistakes:
  • Using mask <= n instead of mask < 2^n
  • Starting loop from 1 misses empty subset
  • Using mask < n only covers single elements
3. What is the output of the following TypeScript code?
const arr = ["a", "b", "c"];
const subsets: string[][] = [];
const n = arr.length;
for (let mask = 0; mask < (1 << n); mask++) {
  const subset: string[] = [];
  for (let i = 0; i < n; i++) {
    if ((mask & (1 << i)) !== 0) {
      subset.push(arr[i]);
    }
  }
  subsets.push(subset);
}
console.log(subsets);
medium
A. [["a"], ["b", "a"], ["c", "b"], ["a", "b", "c"]]
B. [[], ["a"], ["b"], ["a", "b"], ["c"], ["a", "c"], ["b", "c"], ["a", "b", "c"]]
C. [[], ["a", "b", "c"]]
D. [["a"], ["b"], ["c"]]

Solution

  1. Step 1: Understand bitmask iteration

    Loop runs from 0 to 7 (2^3 - 1), generating all subsets of 3 elements.
  2. Step 2: Trace subsets generated

    Each mask selects elements where bit is set, producing all subsets including empty and full set.
  3. Final Answer:

    [[], ["a"], ["b"], ["a", "b"], ["c"], ["a", "c"], ["b", "c"], ["a", "b", "c"]] -> Option B
  4. Quick Check:

    All subsets of 3 elements = [[], ["a"], ["b"], ["a", "b"], ["c"], ["a", "c"], ["b", "c"], ["a", "b", "c"]] [OK]
Hint: Bitmask 0 to 7 covers all subsets of 3 elements [OK]
Common Mistakes:
  • Missing empty subset []
  • Incorrect order or missing subsets
  • Confusing subsets with permutations
4. Identify the error in this TypeScript code for generating subsets using bitmask:
const arr = [1, 2, 3];
const n = arr.length;
for (let mask = 0; mask <= (1 << n); mask++) {
  const subset = [];
  for (let i = 0; i < n; i++) {
    if (mask & (1 << i)) {
      subset.push(arr[i]);
    }
  }
  console.log(subset);
}
medium
A. The outer loop should run while mask < (1 << n), not <=
B. The inner loop should run from i = 1 to n
C. The bitwise AND condition should be == 0 instead of truthy check
D. The subset array should be declared outside the loops

Solution

  1. Step 1: Check outer loop range

    Loop runs from 0 to (1 << n) inclusive, which is one too many iterations (should be less than).
  2. Step 2: Understand consequences

    When mask equals (1 << n), it exceeds the maximum bitmask for subsets, causing an extra invalid iteration.
  3. Final Answer:

    The outer loop should run while mask < (1 << n), not <= -> Option A
  4. Quick Check:

    Loop limit off by one = The outer loop should run while mask < (1 << n), not <= [OK]
Hint: Use mask < 2^n, not <= 2^n [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Changing inner loop range incorrectly
  • Misusing bitwise condition checks
5. Given an array arr = [1, 2, 2], which code modification correctly generates all unique subsets using bitmask technique?
hard
A. Use a Set to store stringified subsets to avoid duplicates
B. Sort the array and skip adding duplicates inside inner loop
C. Increase bitmask range to (1 << (n+1)) to cover duplicates
D. No modification needed; bitmask handles duplicates automatically

Solution

  1. Step 1: Understand duplicates in subsets

    Bitmask generates subsets by position, so duplicates in array cause duplicate subsets.
  2. Step 2: Avoid duplicates using a Set

    Storing subsets as strings in a Set removes duplicates by keeping only unique subsets.
  3. Final Answer:

    Use a Set to store stringified subsets to avoid duplicates -> Option A
  4. Quick Check:

    Set removes duplicate subsets [OK]
Hint: Use Set to filter duplicate subsets from duplicates in array [OK]
Common Mistakes:
  • Assuming bitmask alone removes duplicates
  • Increasing bitmask range unnecessarily
  • Trying to skip duplicates without sorting or tracking
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