The Combination Sum II coding problem is a variation of the first Combination Sum problem with two major changes:
Uber, Microsoft, and Tesla use the Combination Sum II interview question to test a candidate's ability to handle edge cases in backtracking. It requires more sophisticated logic than the first version because you must decide which instances of a duplicate number to include. It’s a perfect test of whether a candidate can "de-duplicate" a search space efficiently.
The pattern is Backtracking with Sorting and Deduplication:
Candidates: [10, 1, 2, 7, 6, 1, 5], Target: 8
Sorted: [1, 1, 2, 5, 6, 7, 10]
1).1). Next combinations must sum to 8 - 1 - 1 = 6.1. To avoid a duplicate search, we skip any candidates[i] == candidates[i-1] if we didn't pick i-1.
This ensures we only start a combination with the first available instance of a duplicate number.cand[i] == cand[i-1]) only works if the array is sorted.Master the logic of if (i > start && candidates[i] == candidates[i-1]) continue;. This is a standard way to handle duplicates in many backtracking problems (like Subsets II or Permutations II).
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Combination Sum | Medium | Solve | |
| Permutations | Medium | Solve | |
| Combination Sum III | Medium | Solve | |
| Construct the Lexicographically Largest Valid Sequence | Medium | Solve | |
| Permutations III | Medium | Solve |