Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the memo dictionary for caching results.
DSA C#
Dictionary<int, int> memo = new Dictionary<int, int>[1];Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses () instead of curly braces {}
Using square brackets [] which are for arrays or lists
Using angle brackets <> which are for generics
✗ Incorrect
The dictionary is initialized with curly braces {} to create an empty dictionary.
2fill in blank
mediumComplete the code to check if the result for n is already computed in memo.
DSA C#
if (memo.[1](n)) return memo[n];
Attempts:
3 left
💡 Hint
Common Mistakes
Using HasKey which does not exist in C# Dictionary
Using Exists which is for lists
Using Contains which checks for values, not keys
✗ Incorrect
ContainsKey is the correct method to check if a key exists in a Dictionary in C#.
3fill in blank
hardFix the error in the recursive call to use memoization properly.
DSA C#
memo[n] = Fibonacci([1], memo) + Fibonacci(n - 2, memo);
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of n - 1 causes infinite recursion
Using n - 2 twice misses the n - 1 term
Using n + 1 goes beyond the current number
✗ Incorrect
The recursive call should use n - 1 to get the previous Fibonacci number correctly.
4fill in blank
hardFill both blanks to complete the Fibonacci method signature and base case check.
DSA C#
public int Fibonacci([1] n, Dictionary<int, int> memo) { if ([2]) return n; // rest of code }
Attempts:
3 left
💡 Hint
Common Mistakes
Using bool as parameter type instead of int
Checking only n == 1 misses the case n == 0
Using n < 0 which is invalid for Fibonacci
✗ Incorrect
The method takes an int n, and the base case checks if n is less than or equal to 1 to return n.
5fill in blank
hardFill all three blanks to complete the memoization logic inside the Fibonacci method.
DSA C#
if (!memo.ContainsKey(n)) { memo[[1]] = Fibonacci(n - 1, [2]) + Fibonacci([3], memo); } return memo[n];
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 which is incorrect for Fibonacci
Not passing memo in recursive calls
Using wrong keys for memo dictionary
✗ Incorrect
We store the result for n in memo[n], call Fibonacci with n - 1 and pass memo, then call Fibonacci with n - 2 and memo.
