Bird
Raised Fist0

Given the following code to generate subsets, what is the value of result after processing num=2 when nums = [1, 2]?

easy🧾 Code Trace Q12 of Q15
Subsets & Combinations - Subsets Using Bitmask
Given the following code to generate subsets, what is the value of result after processing num=2 when nums = [1, 2]?
A[[], [1], [2]]
B[[], [1], [1, 2], [2]]
C[[], [1], [2], [1, 2], [2, 2]]
D[[], [1], [2], [1, 2]]
Step-by-Step Solution
  1. Step 1: Trace result after processing num=1

    Initially result = [[]]. After num=1, new_subsets = [[1]], result = [[], [1]].
  2. Step 2: Trace result after processing num=2

    For each subset in [[], [1]], add 2: new_subsets = [[2], [1, 2]]. Extend result: [[], [1], [2], [1, 2]].
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    All subsets of [1,2] are present exactly once [OK]
Quick Trick: Iterative subsets double result size each iteration [OK]
Common Mistakes:
MISTAKES
  • Forgetting to add subsets with current num
  • Appending duplicates
  • Wrong order of subsets
Trap Explanation:
PITFALL
  • Confusing order or missing subsets leads to wrong result; careful iteration is key.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute iterative subset construction code.
Master "Subsets Using Bitmask" in Subsets & Combinations

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Subsets & Combinations Quizzes