Bird
Raised Fist0

Given the following bitmask enumeration code for subsets, what is the output when nums = [1, 2]?

easy🧾 Code Trace Q3 of Q15
Bit Manipulation - Subsets Using Bitmask
Given the following bitmask enumeration code for subsets, what is the output when nums = [1, 2]?
A[[], [2], [1], [1, 2]]
B[[], [1], [2], [1, 2]]
C[[1], [], [2], [1, 2]]
D[[1, 2], [1], [2], []]
Step-by-Step Solution
Solution:
  1. Step 1: Trace mask values for n=2

    mask=0 -> [], mask=1 -> [1], mask=2 -> [2], mask=3 -> [1,2]
  2. Step 2: Confirm output order

    Subsets appended in order of mask from 0 to 3, matching [[], [1], [2], [1, 2]].
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Output matches expected subsets in ascending mask order [OK]
Quick Trick: Mask 0 means empty subset, increasing mask adds elements [OK]
Common Mistakes:
MISTAKES
  • Confusing mask bit order
  • Misordering subsets in output
Trap Explanation:
PITFALL
  • Candidates often confuse bit order or subset order, leading to wrong output choices.
Interviewer Note:
CONTEXT
  • Tests candidate's ability to mentally execute bitmask subset generation.
Master "Subsets Using Bitmask" in Bit Manipulation

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 Bit Manipulation Quizzes