Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the powerset generation by calling the recursive function.
DSA Rust
fn main() {
let nums = vec![1, 2, 3];
let mut result = Vec::new();
let mut subset = Vec::new();
generate_subsets(&nums, 0, &mut subset, &mut result);
println!("{:?}", result);
}
fn generate_subsets(nums: &Vec<i32>, index: usize, subset: &mut Vec<i32>, result: &mut Vec<Vec<i32>>) {
if index == nums.len() {
result.push(subset.clone());
return;
}
// Exclude nums[index]
generate_subsets(nums, [1], subset, result);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the same index causes infinite recursion.
Using index - 1 causes underflow and errors.
✗ Incorrect
We move to the next index by adding 1 to the current index to explore subsets excluding the current element.
2fill in blank
mediumComplete the code to include the current element in the subset before the recursive call.
DSA Rust
fn generate_subsets(nums: &Vec<i32>, index: usize, subset: &mut Vec<i32>, result: &mut Vec<Vec<i32>>) {
if index == nums.len() {
result.push(subset.clone());
return;
}
// Exclude nums[index]
generate_subsets(nums, index + 1, subset, result);
// Include nums[index]
subset.push([1]);
generate_subsets(nums, index + 1, subset, result);
subset.pop();
}Attempts:
3 left
💡 Hint
Common Mistakes
Adding the index number instead of the element.
Using subset or result vectors incorrectly.
✗ Incorrect
We add the current element nums[index] to the subset before exploring subsets that include it.
3fill in blank
hardFix the error in the base case condition to correctly detect when all elements are processed.
DSA Rust
fn generate_subsets(nums: &Vec<i32>, index: usize, subset: &mut Vec<i32>, result: &mut Vec<Vec<i32>>) {
if [1] {
result.push(subset.clone());
return;
}
generate_subsets(nums, index + 1, subset, result);
subset.push(nums[index]);
generate_subsets(nums, index + 1, subset, result);
subset.pop();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using > or >= causes out-of-bound errors.
Using < causes the base case to never trigger.
✗ Incorrect
The base case is when index equals the length of nums, meaning all elements are processed.
4fill in blank
hardFill both blanks to create a comprehension that generates all subsets of a list of numbers.
DSA Rust
fn main() {
let nums = vec![1, 2, 3];
let result: Vec<Vec<i32>> = (0..(1usize << nums.len()))
.map(|mask| {
nums.iter()
.enumerate()
.filter(|(i, _)| (mask [1] (1usize << i)) [2] 0)
.map(|(_, &num)| num)
.collect()
})
.collect();
println!("{:?}", result);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using bitwise OR (|) instead of AND (&) causes wrong filtering.
Using == 0 includes excluded elements instead of included.
✗ Incorrect
Use bitwise AND (&) to check if the bit at position i is set in mask, and compare with 0 using != to include the element.
5fill in blank
hardFill all three blanks to complete the recursive function that generates all subsets by backtracking.
DSA Rust
fn generate_subsets(nums: &Vec<i32>, index: usize, subset: &mut Vec<i32>, result: &mut Vec<Vec<i32>>) {
if index == nums.len() {
result.push(subset.clone());
return;
}
// Exclude current element
generate_subsets(nums, [1], subset, result);
// Include current element
subset.push([2]);
generate_subsets(nums, [3], subset, result);
subset.pop();
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the same index causes infinite recursion.
Using index - 1 causes errors.
Adding wrong element to subset.
✗ Incorrect
We move to the next index by adding 1 for both recursive calls. We add nums[index] to the subset before including it.
