By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(A Complete FAANG-Level Interview Guide)
"This pattern appears in 1 out of every 3 onsite interviews—master it, and you’ll solve backtracking problems with confidence, not panic."
Before tackling Combination Sum, ensure you understand: 1. Backtracking (DFS): Recursive exploration of all possible combinations, pruning invalid paths early. 2. Time Complexity of Recursion: How to analyze exponential-time algorithms (e.g., O(2^n) vs. O(n!)). 3. Hash Sets for Deduplication: Avoiding duplicate combinations when input has duplicates.
O(2^n)
O(n!)
target
[2,3,6,7]
target=7
[2,2,3]
[7]
[1,1,2]
dp[i][j]
j
i
O(n)
n > 30
Mental Checklist for Every Combination Sum Problem:
[2,3]
Is the input sorted? (If not, sort it first for pruning.)
Choose the Right Technique
Two-Pointer/Hash Map: If problem reduces to subarray sums.
Sort the Input (If Unsorted)
Enables pruning (skip numbers > remaining target).
Define the Backtracking Function
start_index
remaining_target
current_path
result
remaining_target == 0
Recursive case:
num > remaining_target
Handle Edge Cases
target = 0
Negative numbers (rare, but clarify with interviewer).
Optimize (If Needed)
(start_index, remaining_target)
Meet-in-the-Middle: For large n, split input into two halves.
n
Test with Examples
target=5
target=3
n=20
target=100
Problem: Given candidates = [2,3,6,7], target = 7, return all unique combinations where numbers sum to target (numbers can be reused).
candidates = [2,3,6,7]
target = 7
index=0
remaining=7
path=[]
2
remaining=5
path=[2]
remaining=3
path=[2,2]
remaining=1
3
remaining=0
6
remaining=-1
7
[[2,2,3], [7]]
def combinationSum(candidates, target): def backtrack(start, remaining, path): if remaining == 0: result.append(path.copy()) return for i in range(start, len(candidates)): num = candidates[i] if num > remaining: continue # Prune path.append(num) backtrack(i, remaining - num, path) # Reuse same number path.pop() # Backtrack result = [] backtrack(0, target, []) return result
O(2^target)
O(target)
num > remaining
i+1
Problem: Given candidates = [10,1,2,7,6,1,5], target = 8, return all unique combinations where numbers cannot be reused and input may have duplicates.
candidates = [10,1,2,7,6,1,5]
target = 8
[1,1,2,5,6,7,10]
1
[[1,1,6], [1,2,5], [1,7], [2,6]]
def combinationSum2(candidates, target): def backtrack(start, remaining, path): if remaining == 0: result.append(path.copy()) return for i in range(start, len(candidates)): if i > start and candidates[i] == candidates[i-1]: continue # Skip duplicates num = candidates[i] if num > remaining: break # Prune (input is sorted) path.append(num) backtrack(i + 1, remaining - num, path) # No reuse path.pop() candidates.sort() result = [] backtrack(0, target, []) return result
i > start
[1,1,6]
Problem: Given nums = [1,2,3], target = 4, return the number of possible combinations (order matters, e.g., [1,1,1,1] and [1,3] are different).
nums = [1,2,3]
target = 4
[1,1,1,1]
[1,3]
dp[i]
dp[0] = 1
dp[i] += dp[i - num]
num <= i
dp[4] = 7
[1,2,1]
[2,1,1]
[2,2]
[3,1]
def combinationSum4(nums, target): dp = [0] (target + 1) dp[0] = 1 # Base case for i in range(1, target + 1): for num in nums: if num <= i: dp[i] += dp[i - num] return dp[target]
O(target n)
O(n^target)
nums
if i > start and nums[i] == nums[i-1]
target=0
[]
dp[i + offset]
"Alright, let’s lock this in. For Combination Sum problems, here’s your 30-second cheat sheet:
num
nums[i] == nums[i-1]
Test with small inputs first. If the interviewer asks for optimizations, think memoization or meet-in-the-middle. You’ve got this!
Now go crush that interview! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.