PythonStudio
Interview Problems

Combination Sum

medium · backtracking

candidates contains distinct positive integers. Return combinations summing to target >= 0, reusing a candidate any number of times. Sort values within each combination ascending and return the outer list in Python lexicographic list order; no duplicate combinations. target <= 30 with at most 8 candidates. Target 0 has exactly one combination, []. Examples (JSON; null means Python None): Input: {"candidates": [2, 3, 6, 7], "target": 7} Output: [[2, 2, 3], [7]] Input: {"candidates": [2, 3, 5], "target": 8} Output: [[2, 2, 2, 2], [2, 3, 3], [3, 5]]

Starter code

def combination_sum(candidates: list[int], target: int) -> list[list[int]]:
    """Enumerate nondecreasing reusable-candidate combinations."""
    pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python compiler