Bird
Raised Fist0

Consider the following Python code for generating subsets with duplicates. What is the value of res after processing the input [1, 2, 2]?

easy🧾 Code Trace Q12 of Q15
Subsets & Combinations - Subsets II (With Duplicates)
Consider the following Python code for generating subsets with duplicates. What is the value of res after processing the input [1, 2, 2]?
A[[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]
B[[], [1], [2], [1, 2], [2, 2], [1, 2, 2], [2]]
C[[], [1], [2], [1, 2], [2, 2], [1, 2, 2], [1]]
D[[], [1], [2], [1, 2], [2, 2], [1, 2, 2], [1, 2]]
Step-by-Step Solution
Solution:
  1. Step 1: Trace iterations for input [1, 2, 2]

    Start with res = [[]]. - i=0 (num=1): s=0, start=1, add [1] -> res = [[], [1]] - i=1 (num=2): s=0, start=2, add [2], [1,2] -> res = [[], [1], [2], [1,2]] - i=2 (num=2): duplicate, s=start from previous iteration=2, start=4, add [2,2], [1,2,2] -> res = [[], [1], [2], [1,2], [2,2], [1,2,2]]
  2. Step 2: Confirm final res matches option

    The final res contains exactly these subsets without duplicates: [], [1], [2], [1,2], [2,2], [1,2,2].
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Output matches expected unique subsets for input [1,2,2] [OK]
Quick Trick: Duplicate skips start index to avoid repeats [OK]
Common Mistakes:
MISTAKES
  • Appending duplicates multiple times by resetting s=0 always
  • Off-by-one errors in start index
  • Including extra subsets from previous iterations
Trap Explanation:
PITFALL
  • Confusing when to reset start index causes duplicates or missing subsets.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute backtracking with duplicate skipping.
Master "Subsets II (With Duplicates)" 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