Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the memo dictionary for memoization.
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
Assigning null instead of initializing
✗ Incorrect
In C#, a 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 Contains which checks for values, not keys
Using Exists which is not a Dictionary method
✗ Incorrect
The Dictionary method ContainsKey checks if a key exists in the dictionary.
3fill in blank
hardFix the error in the recursive call to compute Fibonacci with memoization.
DSA C#
memo[n] = Fibonacci([1] - 1, memo) + Fibonacci(n - 2, memo);
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of n - 1 causes infinite recursion
Using n - 2 in both calls is incorrect
Using n + 1 increases n and causes errors
✗ Incorrect
The first recursive call should be Fibonacci(n - 1, memo) to correctly compute Fibonacci numbers.
4fill in blank
hardFill both blanks to complete the memoization check and recursive call in Fibonacci.
DSA C#
if (memo.[1](n)) return memo[n]; memo[n] = Fibonacci(n - 1, memo) [2] Fibonacci(n - 2, memo);
Attempts:
3 left
💡 Hint
Common Mistakes
Using Add instead of ContainsKey for checking
Using - instead of + to combine results
Not checking memo before recursive calls
✗ Incorrect
Use ContainsKey to check memo and + to add the two recursive Fibonacci calls.
5fill in blank
hardFill all three blanks to complete the Fibonacci memoization function.
DSA C#
public int Fibonacci(int n, Dictionary<int, int> memo) {
if (n <= 1) return n;
if (memo.[1](n)) return memo[n];
memo[n] = Fibonacci(n [2] 1, memo) [3] Fibonacci(n - 2, memo);
return memo[n];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of + to combine results
Using + instead of - in recursive call
Not checking memo before recursion
✗ Incorrect
Check memo with ContainsKey, subtract 1 from n in first call, and add results with +.
