Memoization is a technique to store the results of expensive function calls and reuse them when the same inputs occur again, avoiding repeated calculations.
Click to reveal answer
intermediate
How does memoization improve recursive function performance?
By saving results of subproblems, memoization prevents the function from recalculating the same values multiple times, reducing time complexity from exponential to polynomial in many cases.
Click to reveal answer
beginner
In C#, which data structure is commonly used to implement memoization?
A Dictionary is commonly used to store computed results with input parameters as keys and results as values.
Click to reveal answer
intermediate
What is the difference between top-down memoization and bottom-up dynamic programming?
Top-down memoization uses recursion with caching results, while bottom-up dynamic programming solves smaller subproblems first iteratively and builds up to the solution.
Click to reveal answer
beginner
Why is memoization especially useful for problems like Fibonacci sequence calculation?
Because naive recursion recalculates the same Fibonacci numbers many times, memoization stores these results once computed, drastically reducing redundant work.
Click to reveal answer
What does memoization store to optimize recursion?
AResults of previous function calls
BInput parameters only
CAll possible outputs in advance
DRandom values to speed up
✗ Incorrect
Memoization stores the results of previous function calls to avoid repeated calculations.
Which C# data structure is best suited for memoization?
AList<T>
BDictionary<TKey, TValue>
CQueue<T>
DStack<T>
✗ Incorrect
Dictionary allows fast lookup of stored results by input keys.
Memoization changes the time complexity of naive Fibonacci recursion from:
AO(2^n) to O(n)
BO(n) to O(1)
CO(n^2) to O(n^3)
DO(n!) to O(n^2)
✗ Incorrect
Memoization reduces exponential time O(2^n) to linear time O(n) for Fibonacci.
Which statement is true about memoization?
AIt always uses iteration instead of recursion.
BIt only works for sorting algorithms.
CIt increases the number of recursive calls.
DIt stores intermediate results to avoid recomputation.
✗ Incorrect
Memoization stores intermediate results to avoid recomputation in recursion.
What is a key benefit of memoization in recursive algorithms?
AUses less memory than recursion alone.
BSimplifies code by removing recursion.
CPrevents repeated calculations of the same inputs.
DEliminates the need for base cases.
✗ Incorrect
Memoization prevents repeated calculations by caching results.
Explain how memoization works to optimize a recursive function like Fibonacci calculation.
Think about saving answers to smaller problems to reuse later.
You got /4 concepts.
Describe the difference between memoization and bottom-up dynamic programming.
Consider how the problem is solved step-by-step in each method.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using memoization in recursive functions?
easy
A. To make the function run slower by adding extra steps
B. To avoid using any variables inside the function
C. To convert recursion into iteration automatically
D. To store results of expensive function calls and reuse them
Solution
Step 1: Understand recursion inefficiency
Recursive functions often repeat the same calculations multiple times, which wastes time.
Step 2: Role of memoization
Memoization saves the results of these repeated calculations so they can be reused instead of recalculated.
Final Answer:
To store results of expensive function calls and reuse them -> Option D
Quick Check:
Memoization = Store and reuse results [OK]
Hint: Memoization saves repeated results to speed recursion [OK]
Common Mistakes:
Thinking memoization slows down recursion
Confusing memoization with iteration
Believing memoization removes recursion
2. Which of the following is the correct way to declare a memo dictionary for integer keys and values in C#?
Hint: Recall Fibonacci sequence values or trace memoized calls [OK]
Common Mistakes:
Confusing Fib(5) with Fib(6)
Ignoring memoization effect
Miscounting base cases
4. Identify the error in this memoized factorial function and select the fix:
static Dictionary memo = new Dictionary();
static int Factorial(int n) {
if (n == 0) return 1;
if (memo.ContainsKey(n)) return memo[n];
memo[n] = n * Factorial(n - 1);
return n;
}
medium
A. Use memo[n] = Factorial(n - 1) * (n - 1);
B. Return memo[n] instead of n after recursive call
C. Remove memo dictionary completely
D. Change base case to if (n == 1) return 1;
Solution
Step 1: Analyze return statement after recursion
The function returns 'n' instead of the computed factorial stored in memo[n], which is incorrect.
Step 2: Correct return value
Return memo[n] to return the calculated factorial value after storing it.
Final Answer:
Return memo[n] instead of n after recursive call -> Option B
Quick Check:
Return memo[n] fixes incorrect output [OK]
Hint: Return stored memo value, not input parameter [OK]
Common Mistakes:
Returning input parameter instead of memoized result
Changing base case unnecessarily
Misusing memo dictionary keys
5. You want to optimize a recursive function that calculates the number of ways to climb n stairs taking 1 or 2 steps at a time. Which memoization approach below correctly implements this in C#?
hard
A. Use a Dictionary to store results; base cases for 0 and 1 stairs return 1; recursive call sums ways(n-1) and ways(n-2).
B. Use a List<int> to store results; base case returns 0 for 0 stairs; recursive call multiplies ways(n-1) and ways(n-2).
C. Use a Dictionary but no base cases; recursive call sums ways(n-1) and ways(n-2).
D. Use an array of size n; base cases return n; recursive call subtracts ways(n-1) from ways(n-2).
Solution
Step 1: Understand problem and base cases
Number of ways to climb stairs with 1 or 2 steps: ways(0)=1 (one way to stand still), ways(1)=1.
Step 2: Correct memoization approach
Use Dictionary to store results; recursive call sums ways(n-1) and ways(n-2) to count all paths.
Final Answer:
Use a Dictionary to store results; base cases for 0 and 1 stairs return 1; recursive call sums ways(n-1) and ways(n-2). -> Option A
Quick Check:
Memo + base cases + sum recursion = Use a Dictionary to store results; base cases for 0 and 1 stairs return 1; recursive call sums ways(n-1) and ways(n-2). [OK]
Hint: Base cases 0,1 = 1; sum ways(n-1) + ways(n-2) with memo [OK]
Common Mistakes:
Wrong base cases values
Using multiplication instead of addition
Missing base cases causing infinite recursion
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.