Single-Use Combination Sum
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace
Problem
Implement Solution.combinationSum2(candidates, target). Each input position may be used once. Return unique value combinations totaling target; output order does not matter.
Starter code
class Solution:
def combinationSum2(self, candidates, target):
passTest cases
duplicate-input
{
"args": [
[
10,
1,
2,
7,
6,
1,
5
],
8
]
}Expected: [[1,1,6],[1,2,5],[1,7],[2,6]]
Wizard outline
- Step 1: Initialize Solution.combinationSum2
Replace the empty starter with the first real state owned by Solution.combinationSum2. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.
- Step 2: Assemble the primary transition
Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.
- Step 3: Pass the No Solution case
Complete the readable core algorithm for one representative Interview case. Sort and skip equal candidates only when they compete at the same recursion depth.
- Step 4: Harden the Duplicate Input boundary
Repair the reviewed boundary and pass the complete submission contract. Advancing the index uses each position at most once. At a fixed depth, equal values create identical suffix searches, so skipping later copies removes duplicates while copies at deeper levels remain available.
Footguns and prerequisites
- Skipping every repeated value globally would remove combinations that legitimately contain two copies.
- recursion and backtracking
Reviewed references
Practice prerequisites
- Complete One Backtracking Frame(opens in a new tab)
Complete One Backtracking Frame isolates before expanding each choice, path equals the original caller-owned prefix; every emitted candidate contains exactly one additional element. That focused state discipline is required when implementing combination sum two as a complete Interview Problem.
Recommended approach and implementation
Sort candidates, recurse from the next index after each choice, and skip equal values following the first candidate at each depth.
Why it works: Advancing the index uses each position at most once. At a fixed depth, equal values create identical suffix searches, so skipping later copies removes duplicates while copies at deeper levels remain available.
class Solution:
def combinationSum2(self, candidates, target):
"""
Checkpoint 1: initialize the state owned by this Interview contract.
Checkpoint 2: assemble the primary transition without hiding the boundary.
"""
candidates.sort()
result = []
path = []
def backtrack(start, remaining):
if remaining == 0:
result.append(path[:])
return
for index in range(start, len(candidates)):
if index > start and candidates[index] == candidates[index - 1]:
continue
value = candidates[index]
if value > remaining:
break
path.append(value)
backtrack(index + 1, remaining - value)
path.pop()
backtrack(0, target)
return result