Bird
Raised Fist0

Given the following iterative code to generate all combinations of size k from 1 to n, what is the output of combine(4, 2)?

easy🧾 Code Trace Q3 of Q15
Subsets & Combinations - Combinations (Choose K from N)
Given the following iterative code to generate all combinations of size k from 1 to n, what is the output of combine(4, 2)?
A[[1, 2], [2, 3], [3, 4]]
B[[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]
C[[1, 2], [1, 3], [2, 3], [2, 4], [3, 4]]
D[[1, 2], [1, 3], [1, 4], [2, 3], [3, 4]]
Step-by-Step Solution
Solution:
  1. Step 1: Trace initial combination

    Start with [1, 2], add to result.
  2. Step 2: Increment rightmost element possible

    Generate [1, 3], [1, 4], then increment first element to 2 and reset second: [2, 3], [2, 4], finally [3, 4].
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    All 6 combinations of 4 choose 2 are generated [OK]
Quick Trick: Iterative lex order generates all combinations in ascending order [OK]
Common Mistakes:
MISTAKES
  • Missing some combinations
  • Off-by-one in increments
Trap Explanation:
PITFALL
  • Candidates often miss resetting elements after increment causing missing combos.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute iterative combinations generation code.
Master "Combinations (Choose K from N)" 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