Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the Fibonacci number at position n using recursion.
DSA C#
public 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 sequence is defined as the sum of the two previous numbers, so we add Fibonacci(n - 1) and Fibonacci(n - 2).
2fill in blank
mediumComplete the code to store computed Fibonacci values in a dictionary to avoid repeated calculations.
DSA C#
private Dictionary<int, int> memo = new Dictionary<int, int>();
public int FibonacciMemo(int n) {
if (n <= 1) return n;
if (memo.ContainsKey(n)) return memo[n];
memo[n] = FibonacciMemo(n - 1) [1] FibonacciMemo(n - 2);
return memo[n];
}Attempts:
3 left
💡 Hint
Common Mistakes
Not adding the two recursive calls.
Not storing the result in the dictionary.
✗ Incorrect
We add the results of FibonacciMemo(n - 1) and FibonacciMemo(n - 2) and store it in memo to avoid recalculations.
3fill in blank
hardFix the error in the recursive factorial function to correctly compute factorial of n.
DSA C#
public int Factorial(int n) {
if (n == 0) return 1;
return n [1] Factorial(n - 1);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Forgetting the base case.
✗ Incorrect
Factorial is the product of n and factorial of (n-1), so we use the multiplication operator '*'.
4fill in blank
hardFill both blanks to create a dictionary comprehension that stores squares of numbers from 1 to 5 only if the number is even.
DSA C#
var squares = new Dictionary<int, int>(); for (int i = 1; i <= 5; i++) { if (i [1] 2 == 0) { squares[i] = i [2] i; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '-' instead of '%' for even check.
Using '+' instead of '*' for squaring.
✗ Incorrect
We use '%' to check if i is even (remainder 0) and '*' to compute the square of i.
5fill in blank
hardFill all three blanks to create a dictionary that stores uppercase keys and their values only if the value is greater than 10.
DSA C#
var filteredDict = new Dictionary<string, int>(); foreach (var kvp in data) { if (kvp.Value [1] 10) { filteredDict[kvp.Key.[2]()] = kvp.[3]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for comparison.
Using 'Key' instead of 'Value' for dictionary value.
Not converting key to uppercase.
✗ Incorrect
We check if value is greater than 10 using '>', convert key to uppercase with 'ToUpper', and access value with 'Value'.
