Challenge - 5 Problems
Combination Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of combinations summing to K
What is the output of the following code that generates all combinations of numbers from 1 to 4 that sum to 5?
DSA Python
def comb_sum_k(n, k): result = [] def backtrack(start, path, total): if total == k: result.append(path[:]) return if total > k: return for i in range(start, n+1): path.append(i) backtrack(i+1, path, total + i) path.pop() backtrack(1, [], 0) return result print(comb_sum_k(4, 5))
Attempts:
2 left
💡 Hint
Think about how the backtracking avoids duplicates and only uses numbers once.
✗ Incorrect
The code generates combinations of unique numbers from 1 to 4 that sum to 5. Only [1,4] and [2,3] satisfy this with unique numbers and no repeats.
❓ Predict Output
intermediateNumber of combinations summing to K
How many unique combinations of numbers from 1 to 5 sum to 7 using the code below?
DSA Python
def comb_sum_k(n, k): result = [] def backtrack(start, path, total): if total == k: result.append(path[:]) return if total > k: return for i in range(start, n+1): path.append(i) backtrack(i+1, path, total + i) path.pop() backtrack(1, [], 0) return result print(len(comb_sum_k(5, 7)))
Attempts:
2 left
💡 Hint
List out all combinations manually or think about subsets of 1 to 5 that sum to 7.
✗ Incorrect
The valid combinations are [1,2,4], [2,5], [3,4]. Total of 3 combinations.
🔧 Debug
advancedIdentify the error in combination sum code
What error does the following code raise when trying to generate combinations summing to 4?
DSA Python
def comb_sum_k(n, k): result = [] def backtrack(start, path, total): if total == k: result.append(path[:]) return if total > k: return for i in range(start, n): path.append(i) backtrack(i+1, path, total + i) path.pop() backtrack(1, [], 0) return result print(comb_sum_k(4, 4))
Attempts:
2 left
💡 Hint
Check the range used in the for loop and if it includes the last number.
✗ Incorrect
The for loop uses range(start, n) which excludes n. So number n is never included, missing combinations with n.
🧠 Conceptual
advancedWhy use backtracking for combination sum?
Why is backtracking a suitable approach to generate all combinations of numbers summing to K?
Attempts:
2 left
💡 Hint
Think about how backtracking avoids unnecessary work.
✗ Incorrect
Backtracking tries all possible combinations but stops exploring a path as soon as the sum exceeds K, saving time.
🚀 Application
expertFind all combinations summing to K with duplicates allowed
Modify the code to allow repeated numbers in combinations summing to K from 1 to 3. What is the output for K=4?
DSA Python
def comb_sum_k_with_repeats(n, k): result = [] def backtrack(start, path, total): if total == k: result.append(path[:]) return if total > k: return for i in range(start, n+1): path.append(i) backtrack(i, path, total + i) # allow repeats by not increasing start path.pop() backtrack(1, [], 0) return result print(comb_sum_k_with_repeats(3, 4))
Attempts:
2 left
💡 Hint
Allow repeats by calling backtrack with same start index.
✗ Incorrect
The code allows repeats by calling backtrack with the same start index (backtrack(i, ...)). It generates non-decreasing combinations: [[1,1,1,1], [1,1,2], [1,3], [2,2]].
