Bird
Raised Fist0

The following code attempts to solve Combination Sum II but contains a subtle bug. Identify the line causing the bug.

medium🐞 Bug Identification Q14 of Q15
Subsets & Combinations - Combination Sum II (No Reuse, Duplicates)
The following code attempts to solve Combination Sum II but contains a subtle bug. Identify the line causing the bug.
ALine with 'if i > start and candidates[i] == candidates[i-1]: continue' for duplicate skipping
BLine with 'if candidates[i] > target: break' for pruning
CLine with 'backtrack(i + 1, path + [candidates[i]], target - candidates[i])' recursive call
DLine with 'prev = candidates[i]' inside the loop
Step-by-Step Solution
  1. Step 1: Understand duplicate skipping logic

    The code uses 'if i > start and candidates[i] == candidates[i-1]: continue' to skip duplicates correctly.
  2. Step 2: Identify subtle bug

    The 'prev' variable is assigned but never used, which is redundant and may confuse readers. The actual duplicate skipping is done by the index comparison, so 'prev' should be removed to avoid confusion.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Redundant 'prev' variable assignment is a subtle bug causing confusion [OK]
Quick Trick: Duplicate skipping must be consistent and correct at recursion level; remove unused variables [OK]
Common Mistakes:
MISTAKES
  • Using prev variable incorrectly
  • Skipping duplicates globally instead of per recursion level
  • Not pruning when candidate > target
Trap Explanation:
PITFALL
  • Using both prev and index-based duplicate skipping confuses logic and may skip valid combos.
Interviewer Note:
CONTEXT
  • Tests candidate's ability to spot subtle duplicate skipping bugs in backtracking.
Master "Combination Sum II (No Reuse, 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