Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the DP array with base Fibonacci values.
DSA Rust
let mut dp = vec![0; n + 1]; dp[0] = 0; dp[1] = [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[1] to 0 instead of 1.
Using n instead of 1 for dp[1].
✗ Incorrect
The first Fibonacci number after 0 is 1, so dp[1] should be set to 1.
2fill in blank
mediumComplete the for loop to fill the DP array with Fibonacci numbers.
DSA Rust
for i in 2..=[1] { dp[i] = dp[i - 1] + dp[i - 2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using n - 1 which misses the last Fibonacci number.
Using n + 1 which causes out-of-bound error.
✗ Incorrect
The loop should run from 2 up to n inclusive to fill all Fibonacci numbers up to n.
3fill in blank
hardFix the error in the return statement to return the nth Fibonacci number.
DSA Rust
return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning dp[n - 1] which is the (n-1)th Fibonacci number.
Returning dp[n + 1] which is out of bounds.
✗ Incorrect
The nth Fibonacci number is stored at dp[n].
4fill in blank
hardFill both blanks to complete the function signature and return type.
DSA Rust
fn fibonacci([1]: usize) -> [2] { // function body }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i32 for input or output which can cause negative values.
Using variable names other than n for input.
✗ Incorrect
The function takes n as usize and returns usize for Fibonacci number.
5fill in blank
hardFill all three blanks to complete the DP Fibonacci function body.
DSA Rust
fn fibonacci(n: usize) -> usize {
if n == 0 || n == 1 {
return [1];
}
let mut dp = vec![0; n + 1];
dp[0] = 0;
dp[1] = 1;
for i in 2..=[2] {
dp[i] = dp[i - 1] + [3];
}
dp[n]
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 or 1 directly without checking n.
Using wrong indices in the loop sum.
✗ Incorrect
Return n for base cases 0 and 1; loop to n inclusive; sum dp[i-1] and dp[i-2].
