Bird
Raised Fist0
Interview Prepsubsets-combinationsmediumAmazonGoogle

Combination Sum II (No Reuse, Duplicates)

Choose your preparation mode4 modes available
</>
IDE
def combinationSum2(candidates: List[int], target: int) -> List[List[int]]:public List<List<Integer>> combinationSum2(int[] candidates, int target)vector<vector<int>> combinationSum2(vector<int>& candidates, int target)function combinationSum2(candidates, target)
def combinationSum2(candidates, target):
    # Write your solution here
    pass
class Solution {
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        // Write your solution here
        return new ArrayList<>();
    }
}
#include <vector>
using namespace std;

vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
    // Write your solution here
    return {};
}
function combinationSum2(candidates, target) {
    // Write your solution here
}
Coming soon
0/10
Common Bugs to Avoid
Wrong: [[1,1,6],[1,2,5],[1,7],[2,6],[1,1,6]]Duplicates not skipped at the same recursion depth, causing repeated combinations.✅ Add condition to skip candidates[i] if candidates[i] == candidates[i-1] and i > start.
Wrong: [[1,2,2],[5],[2,2,1]]Order of elements not normalized or duplicates not skipped, causing repeated combinations with different order.✅ Sort candidates and skip duplicates at the same recursion level; ensure output combinations are sorted.
Wrong: [[1,1],[1,1],[1,1],[1,1]]Duplicate combinations generated due to missing duplicate skipping in input with all identical elements.✅ Skip duplicates at the same recursion depth by checking candidates[i] == candidates[i-1].
Wrong: [[2,2,3],[7]]Treating problem as unbounded knapsack, allowing reuse of candidates.✅ Increment start index in recursion to prevent reuse: backtrack(i+1, ...).
Wrong: Timeout or no outputBrute force exponential complexity without pruning or duplicate skipping.✅ Implement pruning by stopping recursion when candidate > remaining target and skip duplicates.
✓
Test Cases
t1_01basic
Input{"candidates":[10,1,2,7,6,1,5],"target":8}
Expected[[1,1,6],[1,2,5],[1,7],[2,6]]

We find all unique combinations that sum to 8. Note that [1,1,6] appears only once despite two '1's in candidates.

t1_02basic
Input{"candidates":[2,5,2,1,2],"target":5}
Expected[[1,2,2],[5]]

Unique combinations summing to 5 are [1,2,2] and [5]. Duplicate 2's handled correctly.

t2_01edge
Input{"candidates":[],"target":7}
Expected[]

Empty candidates array means no combinations possible.

t2_02edge
Input{"candidates":[3],"target":3}
Expected[[3]]

Single element equal to target forms one valid combination.

t2_03edge
Input{"candidates":[1,1,1,1],"target":2}
Expected[[1,1]]

All candidates identical; only one unique combination of two 1's sums to 2.

t2_04edge
Input{"candidates":[5,10,12],"target":3}
Expected[]

Target smaller than smallest candidate means no valid combinations.

t3_01corner
Input{"candidates":[1,2,3,4,5],"target":10}
Expected[[1,2,3,4],[1,4,5],[2,3,5]]

Multiple unique combinations sum to 10 without reuse: [1,2,3,4], [1,4,5], and [2,3,5].

t3_02corner
Input{"candidates":[2,3,6,7],"target":7}
Expected[[7]]

Only single candidate 7 sums to target; no reuse allowed.

t3_03corner
Input{"candidates":[1,1,2,5],"target":4}
Expected[[1,1,2]]

Duplicate 1's handled correctly; only one unique combination sums to 4.

t4_01performance
Input{"candidates":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50],"target":250}
⏱ Performance - must finish in 2000ms

Large input with n=100 candidates and target=250; O(2^n * n) brute force will time out.

Practice

(1/5)
1. Consider the following Python code implementing the optimal backtracking solution for Combination Sum III. What is the final value of res after calling combinationSum3(3, 7)?
easy
A. [[1, 2, 4]]
B. [[1, 3, 3]]
C. [[2, 2, 3]]
D. [[1, 2, 3]]

Solution

  1. Step 1: Trace backtracking calls for k=3, n=7

    Start from 1, try combinations of size 3 summing to 7. Valid combos are [1,2,4] only because 1+2+4=7.
  2. Step 2: Verify no other combos meet criteria

    Other combos like [1,3,3] or [2,2,3] are invalid due to duplicates or sum mismatch.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Only [1,2,4] sums to 7 with 3 distinct numbers [OK]
Hint: Only distinct numbers summing to 7 with size 3 is [1,2,4] [OK]
Common Mistakes:
  • Including duplicates like [1,3,3]
  • Miscounting sum or size
2. You are given a set of distinct positive integers and a target sum. You need to find all unique combinations where the chosen numbers sum to the target. Each number can be chosen multiple times. Which algorithmic approach best guarantees finding all valid combinations efficiently without missing any or producing duplicates?
easy
A. Greedy algorithm that picks the largest candidate repeatedly until the sum is reached or exceeded.
B. Backtracking with sorting and frequency counting to avoid duplicates and prune early when sum exceeds target.
C. Dynamic programming using a 2D table to count the number of combinations without generating them explicitly.
D. Pure brute force recursion that tries all subsets without pruning or sorting.

Solution

  1. Step 1: Understand problem constraints

    The problem requires all unique combinations summing to target, allowing reuse of candidates.
  2. Step 2: Evaluate algorithm suitability

    Greedy fails because it can miss combinations; pure brute force is correct but inefficient; DP counts combinations but doesn't generate them; backtracking with sorting and frequency counting avoids duplicates and prunes search space efficiently.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Backtracking with pruning and frequency counting is the standard approach [OK]
Hint: Backtracking with pruning avoids duplicates and excessive search [OK]
Common Mistakes:
  • Assuming greedy always works for sum problems
  • Confusing counting with generating combinations
3. Consider the following Python code for generating subsets with duplicates. What is the value of res after processing the input [1, 2, 2]?
easy
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]]

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]
Hint: Duplicate skips start index to avoid repeats [OK]
Common Mistakes:
  • Appending duplicates multiple times by resetting s=0 always
  • Off-by-one errors in start index
  • Including extra subsets from previous iterations
4. Given the following code to generate subsets, what is the value of result after processing num=2 when nums = [1, 2]?
easy
A. [[], [1], [2]]
B. [[], [1], [1, 2], [2]]
C. [[], [1], [2], [1, 2], [2, 2]]
D. [[], [1], [2], [1, 2]]

Solution

  1. Step 1: Trace result after processing num=1

    Initially result = [[]]. After num=1, new_subsets = [[1]], result = [[], [1]].
  2. Step 2: Trace result after processing num=2

    For each subset in [[], [1]], add 2: new_subsets = [[2], [1, 2]]. Extend result: [[], [1], [2], [1, 2]].
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    All subsets of [1,2] are present exactly once [OK]
Hint: Iterative subsets double result size each iteration [OK]
Common Mistakes:
  • Forgetting to add subsets with current num
  • Appending duplicates
  • Wrong order of subsets
5. What is the time complexity of the optimal bitmask + trie solution for the Number of Valid Words for Each Puzzle problem, given W words with average length L, and P puzzles?
medium
A. O(W * L + P * M), where M is the average number of trie nodes traversed per puzzle
B. O(W * P * L), since each word is checked against each puzzle
C. O(W * 2^7 + P * 7), enumerating all subsets of puzzle letters for each puzzle
D. O(W * L * 26 + P * 26), traversing trie nodes for all letters

Solution

  1. Step 1: Analyze word insertion

    Each word is converted to a bitmask and inserted into trie in O(L) time, total O(W * L).
  2. Step 2: Analyze puzzle queries

    For each puzzle, dfs traverses trie nodes corresponding to subsets of puzzle letters, average M nodes, total O(P * M).
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Matches known optimal complexity [OK]
Hint: Trie traversal depends on subsets of puzzle letters, not all words [OK]
Common Mistakes:
  • Assuming brute force complexity
  • Confusing 2^7 subsets with trie traversal nodes
  • Ignoring average trie traversal cost
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard