Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the nth Fibonacci number using recursion.
DSA Rust
fn fib(n: u32) -> u32 {
if n <= 1 {
return n;
}
fib(n - 1) + fib([1])
}Attempts:
3 left
💡 Hint
Common Mistakes
Using n - 3 or other values instead of n - 2.
Forgetting to return the sum of two recursive calls.
✗ Incorrect
The Fibonacci number is the sum of the two previous numbers: fib(n-1) + fib(n-2).
2fill in blank
mediumComplete the code to memoize Fibonacci results using a vector.
DSA Rust
fn fib_memo(n: usize, memo: &mut Vec<Option<u32>>) -> u32 {
if let Some(value) = memo[n] {
return value;
}
let result = if n <= 1 {
n as u32
} else {
fib_memo(n - 1, memo) + fib_memo([1], memo)
};
memo[n] = Some(result);
result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect indices like n - 3.
Not storing the computed result in the memo vector.
✗ Incorrect
Memoization stores results of fib(n-1) and fib(n-2). The second call must be fib(n-2).
3fill in blank
hardFix the error in the code to correctly compute the minimum cost path in a grid using dynamic programming.
DSA Rust
fn min_cost_path(cost: &Vec<Vec<u32>>, m: usize, n: usize) -> u32 {
if m == 0 && n == 0 {
return cost[0][0];
}
if m == 0 {
return min_cost_path(cost, 0, n - 1) + cost[0][n];
}
if n == 0 {
return min_cost_path(cost, m - 1, 0) + cost[m][0];
}
let left = min_cost_path(cost, m, n - 1) + cost[m][n];
let up = min_cost_path(cost, m - 1, n) + cost[m][n];
if left < [1] {
left
} else {
[2]
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Comparing left with wrong variable.
Returning left in both branches.
✗ Incorrect
We compare left and up costs and return the smaller one. So if left < up, return left else return up.
4fill in blank
hardFill both blanks to create a dynamic programming table for the coin change problem.
DSA Rust
fn coin_change(coins: &Vec<u32>, amount: usize) -> usize {
let mut dp = vec![amount + 1; amount + 1];
dp[0] = 0;
for i in 1..=amount {
for &coin in coins.iter() {
if coin as usize <= i {
dp[i] = dp[i].[1] dp[i - coin as usize] + 1;
}
}
}
if dp[amount] > amount [2] 0 else dp[amount]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using max instead of min.
Using wrong comparison operator in the if condition.
✗ Incorrect
We update dp[i] with the minimum between current dp[i] and dp[i - coin] + 1. If dp[amount] > amount, return 0 meaning no solution.
5fill in blank
hardFill all three blanks to implement a memoized recursive solution for the longest common subsequence (LCS).
DSA Rust
fn lcs(s1: &str, s2: &str, m: usize, n: usize, memo: &mut Vec<Vec<Option<usize>>>) -> usize {
if m == 0 || n == 0 {
return 0;
}
if let Some(val) = memo[m - 1][n - 1] {
return val;
}
let result = if s1.chars().nth(m - 1) == s2.chars().nth(n - 1) {
1 + lcs(s1, s2, [1], [2], memo)
} else {
std::cmp::max(lcs(s1, s2, m - 1, n, memo), lcs(s1, s2, m, [3], memo))
};
memo[m - 1][n - 1] = Some(result);
result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices in recursive calls.
Not memoizing results properly.
✗ Incorrect
When characters match, move both indices back by 1. When not, compare lcs(m-1, n) and lcs(m, n-1).
