Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to calculate Fibonacci numbers using simple recursion.
DSA C#
int Fibonacci(int n) {
if (n <= 1) return n;
return Fibonacci(n - 1) [1] Fibonacci(n - 2);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to return the sum.
✗ Incorrect
The Fibonacci number is the sum of the two previous Fibonacci numbers, so we use the '+' operator.
2fill in blank
easyComplete the code to calculate Fibonacci numbers using simple recursion.
DSA C#
int Fibonacci(int n) {
if (n <= 1) return n;
int a = Fibonacci(n - 1);
int b = Fibonacci(n - 2);
return a [1] b;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Returning only one recursive call.
✗ Incorrect
The Fibonacci number is the sum of the two previous numbers, so '+' is the correct operator.
3fill in blank
mediumComplete the code to add memoization to the Fibonacci function to avoid repeated calculations.
DSA C#
int FibonacciMemo(int n, Dictionary<int, int> memo) {
if (memo.ContainsKey(n)) return memo[n];
if (n <= 1) return n;
memo[n] = FibonacciMemo(n - 1, memo) [1] FibonacciMemo(n - 2, memo);
return memo[n];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong operators in the sum.
Not storing the result in the memo dictionary.
✗ Incorrect
Memoization stores results of Fibonacci sums to avoid repeated work, so '+' is used to add the two previous results.
4fill in blank
hardFill both blanks to create a dynamic programming solution that builds Fibonacci numbers bottom-up.
DSA C#
int FibonacciDP(int n) {
int[] dp = new int[n + 1];
dp[0] = 0;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i [1] 1] [2] dp[i - 2];
}
return dp[n];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of subtraction for indices.
Using wrong operators for summing values.
✗ Incorrect
We subtract 1 from i to get the previous Fibonacci number and add it to the one before that.
5fill in blank
hardFill all three blanks to create a dictionary that filters and stores Fibonacci numbers greater than 5.
DSA C#
var fibNumbers = new Dictionary<int, int>(); for (int i = 0; i <= 10; i++) { int val = FibonacciDP(i); if (val [1] 5) { fibNumbers[[2]] = [3]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Swapping key and value in the dictionary assignment.
✗ Incorrect
We check if Fibonacci value is greater than 5, then store it with key i and value val.
