Bird
Raised Fist0

Consider the following Python code that generates all subsets of nums = [1, 2, 3]. What is the value of subset when mask = 5 during the iteration?

easy🧾 Code Trace Q12 of Q15
Subsets & Combinations - Subsets
Consider the following Python code that generates all subsets of nums = [1, 2, 3]. What is the value of subset when mask = 5 during the iteration?
A[1, 3]
B[2, 3]
C[1, 2]
D[3]
Step-by-Step Solution
  1. Step 1: Convert mask=5 to binary

    5 in binary is 101, meaning bits 0 and 2 are set.
  2. Step 2: Map bits to indices in nums

    Bit 0 corresponds to nums[0]=1, bit 2 corresponds to nums[2]=3, so subset = [1, 3].
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Mask 5 selects elements at positions 0 and 2 -> [1, 3] [OK]
Quick Trick: Mask bits correspond to included elements [OK]
Common Mistakes:
MISTAKES
  • Off-by-one bit indexing
  • Confusing mask bits with element values
Trap Explanation:
PITFALL
  • Candidates often misinterpret bit positions or confuse mask with subset values.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute bitmask subset generation code.
Master "Subsets" 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