Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the recursive function call for generating combinations.
DSA Rust
fn combination_sum_k(k: usize, n: usize) -> Vec<Vec<usize>> {
let mut result = Vec::new();
let mut current = Vec::new();
backtrack(1, k, n, &mut current, &mut result);
result
}
fn backtrack(start: usize, k: usize, n: usize, current: &mut Vec<usize>, result: &mut Vec<Vec<usize>>) {
if n == 0 && current.len() == k {
result.push(current.clone());
return;
}
if current.len() > k || n < 0 {
return;
}
for i in start..=9 {
current.push(i);
backtrack(i + 1, k, n - i, current, result);
current.pop();
}
}
fn main() {
let combinations = combination_sum_k([1], 7);
println!("{:?}", combinations);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong value for k in the initial call.
Confusing the sum target with the number of elements.
✗ Incorrect
The function call starts with k=2 to find combinations of 2 numbers that sum to 7.
2fill in blank
mediumComplete the code to check if the current combination is valid and add it to the result.
DSA Rust
fn backtrack(start: usize, k: usize, n: usize, current: &mut Vec<usize>, result: &mut Vec<Vec<usize>>) {
if n == 0 && current.len() == [1] {
result.push(current.clone());
return;
}
if current.len() > k || n < 0 {
return;
}
for i in start..=9 {
current.push(i);
backtrack(i + 1, k, n - i, current, result);
current.pop();
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Checking against n instead of k.
Using start or result length incorrectly.
✗ Incorrect
We check if the current combination length equals k to ensure correct size before adding.
3fill in blank
hardFix the error in the loop range to correctly iterate over numbers 1 to 9.
DSA Rust
for i in [1] { current.push(i); backtrack(i + 1, k, n - i, current, result); current.pop(); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using exclusive range which excludes 9.
Starting from 1 instead of start.
✗ Incorrect
Using start..=9 includes the number 9 in the loop, which is required.
4fill in blank
hardFill both blanks to correctly handle the base cases in the backtrack function.
DSA Rust
fn backtrack(start: usize, k: usize, n: isize, current: &mut Vec<usize>, result: &mut Vec<Vec<usize>>) {
if n == 0 && current.len() == [1] {
result.push(current.clone());
return;
}
if current.len() > [2] || n < 0 {
return;
}
for i in start..=9 {
current.push(i);
backtrack(i + 1, k, n - i as isize, current, result);
current.pop();
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for length checks.
Confusing n with k.
✗ Incorrect
Both blanks should be 'k' to check combination size correctly.
5fill in blank
hardFill all three blanks to correctly convert and check the remaining sum in the recursive call.
DSA Rust
for i in start..=9 { current.push(i); backtrack(i + 1, k, n [1] i as [2], current, result); current.pop(); } fn backtrack(start: usize, k: usize, n: [3], current: &mut Vec<usize>, result: &mut Vec<Vec<usize>>) { if n == 0 && current.len() == k { result.push(current.clone()); return; } if current.len() > k || n < 0 { return; } for i in start..=9 { current.push(i); backtrack(i + 1, k, n - i as isize, current, result); current.pop(); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction.
Casting to usize instead of isize.
Mismatch in n's type.
✗ Incorrect
We subtract i cast to isize from n which is of type isize.
