Challenge - 5 Problems
Combination Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of generating combinations summing to K
What is the output of the following Rust code that generates all combinations of numbers from 1 to 4 that sum to 5?
DSA Rust
fn generate_combinations_sum_k(n: i32, k: i32) -> Vec<Vec<i32>> {
fn backtrack(start: i32, k: i32, path: &mut Vec<i32>, res: &mut Vec<Vec<i32>>) {
if k == 0 {
res.push(path.clone());
return;
}
for i in start..=4 {
if i > k {
break;
}
path.push(i);
backtrack(i + 1, k - i, path, res);
path.pop();
}
}
let mut res = Vec::new();
let mut path = Vec::new();
backtrack(1, k, &mut path, &mut res);
res
}
fn main() {
let result = generate_combinations_sum_k(4, 5);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Think about combinations of unique numbers from 1 to 4 that add up to 5.
✗ Incorrect
The code generates all unique combinations of numbers from 1 to 4 that sum to 5 without repetition. The valid combinations are [1,4] and [2,3].
🧠 Conceptual
intermediateNumber of combinations summing to K
How many unique combinations of numbers from 1 to 5 sum to 7?
Attempts:
2 left
💡 Hint
Try to list all combinations or think about subsets of {1,2,3,4,5} that sum to 7.
✗ Incorrect
The unique combinations are [2,5], [3,4], [1,2,4], [1,3,3] (invalid since 3 repeats), [1,6] (6 not in range), so valid are [2,5], [3,4], [1,2,4]. The valid unique combinations are 6 in total.
🔧 Debug
advancedIdentify the error in combination sum code
What error will the following Rust code produce when generating combinations summing to 4?
fn generate_combinations(k: i32) -> Vec> {
let mut res = Vec::new();
let mut path = Vec::new();
fn backtrack(start: i32, k: i32, path: &mut Vec, res: &mut Vec>) {
if k == 0 {
res.push(path.clone());
return;
}
for i in start..=4 {
if i > k {
break;
}
path.push(i);
backtrack(i, k - i, path, res);
path.pop();
}
}
backtrack(1, k, &mut path, &mut res);
res
}
Attempts:
2 left
💡 Hint
Look at the recursive call's start parameter.
✗ Incorrect
The recursive call uses 'backtrack(i, ...)' instead of 'backtrack(i + 1, ...)'. This causes the same number to be reused infinitely, leading to infinite recursion and stack overflow.
📝 Syntax
advancedIdentify the syntax error in combination sum code
Which option contains the correct Rust syntax to define a backtracking function that generates combinations summing to k?
Attempts:
2 left
💡 Hint
Check parameter types and mutability.
✗ Incorrect
Option B correctly uses colons to specify types and mutable references for path and res. Option B misses colons. Option B passes vectors by value, which is inefficient and incorrect for backtracking. Option B uses immutable references, so path.pop() would fail.
🚀 Application
expertFind all combinations summing to K with duplicates allowed
Given the numbers [2,3,6,7], which option shows all unique combinations where numbers can be chosen multiple times to sum to 7?
Attempts:
2 left
💡 Hint
Numbers can be reused, but combinations must be unique and sum to 7.
✗ Incorrect
The valid combinations are [7] and [2,2,3]. Option A includes [2,3,3] which sums to 8, option A includes [3,3,3] which sums to 9, and option A includes 4 which is not in the list.
