Memoization is a technique to store the results of expensive recursive calls and reuse them when the same inputs occur again, avoiding repeated calculations.
Click to reveal answer
beginner
How does memoization improve the performance of recursive functions?
By saving the 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
intermediate
In Java, which data structure is commonly used to implement memoization for recursion?
A HashMap or an array is commonly used to store previously computed results keyed by input parameters for quick lookup.
Click to reveal answer
intermediate
What is the difference between memoization and tabulation?
Memoization is a top-down approach that uses recursion and caches results, while tabulation is a bottom-up approach that builds a table iteratively without recursion.
Click to reveal answer
beginner
Why is memoization especially useful in problems like Fibonacci sequence calculation?
Because Fibonacci calculation has overlapping subproblems, memoization stores results of smaller Fibonacci numbers so they are not recalculated multiple times, greatly improving efficiency.
Click to reveal answer
What does memoization primarily help to avoid in recursive functions?
AUsing loops instead of recursion
BRepeated calculations of the same subproblems
CStack overflow errors
DMemory leaks
✗ Incorrect
Memoization stores results of subproblems to avoid recalculating them, which reduces repeated work.
Which data structure is best suited for memoization when input parameters are integers within a small range?
AQueue
BLinked List
CArray
DStack
✗ Incorrect
Arrays provide fast access by index and are ideal when input parameters are small integers.
Memoization is an example of which programming technique?
ABacktracking
BGreedy Algorithm
CDivide and Conquer
DDynamic Programming
✗ Incorrect
Memoization is a top-down dynamic programming technique that caches results of subproblems.
What is the time complexity improvement when using memoization for Fibonacci numbers?
AFrom exponential to linear
BFrom linear to exponential
CFrom quadratic to cubic
DNo improvement
✗ Incorrect
Memoization reduces Fibonacci calculation from exponential time to linear time.
Which of the following is NOT a characteristic of memoization?
AIterative without recursion
BStores intermediate results
CTop-down approach
DImproves recursive function efficiency
✗ Incorrect
Memoization uses recursion (top-down), while iterative without recursion describes tabulation.
Explain how memoization optimizes a recursive function using a real-life example.
Think about how remembering past answers saves time.
You got /4 concepts.
Describe the steps to add memoization to a recursive Java function.
Focus on caching and lookup process.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using memoization in recursive functions?
easy
A. To store results of expensive function calls and reuse them
B. To make the code shorter and simpler
C. To avoid using loops in recursion
D. To increase the number of recursive calls
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 A
Quick Check:
Memoization = Store and reuse results [OK]
Hint: Memoization saves repeated results to speed recursion [OK]
Common Mistakes:
Thinking memoization makes code shorter
Confusing memoization with iteration
Believing memoization increases recursion calls
2. Which of the following Java code snippets correctly declares a memoization cache for storing integer results by input integer keys?
easy
A. Map memo = new HashMap<>();
B. List<Integer> memo = new ArrayList<>();
C. int[] memo = new int();
D. HashSet<Integer> memo = new HashSet<>();
Solution
Step 1: Identify suitable data structure for key-value pairs
Memoization requires storing results by input keys, so a Map is ideal.
Step 2: Check Java syntax correctness
Map memo = new HashMap<>(); uses Map with HashMap, which is correct syntax for memoization cache.
Final Answer:
Map memo = new HashMap<>(); -> Option A
Quick Check:
Memo cache = Map for key-value pairs [OK]
Hint: Use Map for key-value memo cache in Java [OK]
Common Mistakes:
Using List instead of Map for memoization
Incorrect array declaration syntax
Using Set which stores only keys, no values
3. What will be the output of the following Java code using memoization for Fibonacci numbers?
import java.util.*;
public class FibMemo {
static Map memo = new HashMap<>();
public static int fib(int n) {
if (n <= 1) return n;
if (memo.containsKey(n)) return memo.get(n);
int result = fib(n-1) + fib(n-2);
memo.put(n, result);
return result;
}
public static void main(String[] args) {
System.out.println(fib(5));
}
}
medium
A. Error at runtime
B. 8
C. 5
D. 13
Solution
Step 1: Understand Fibonacci sequence calculation
fib(5) = fib(4) + fib(3), and so on, with base cases fib(0)=0, fib(1)=1.
Step 2: Calculate fib(5) with memoization
fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=5. Memoization avoids repeated calls but result is same.
Final Answer:
5 -> Option C
Quick Check:
fib(5) = 5 [OK]
Hint: Recall Fibonacci(5) = 5, memoization doesn't change result [OK]
Common Mistakes:
Confusing Fibonacci values for wrong n
Assuming memoization changes output
Thinking code causes runtime error
4. Identify the error in this Java memoized factorial function:
import java.util.*;
public class FactorialMemo {
static Map memo = new HashMap<>();
public static int fact(int n) {
if (n == 0) return 1;
if (memo.containsKey(n)) return memo.get(n);
int result = n * fact(n - 1);
memo.put(n, result);
return result;
}
public static void main(String[] args) {
System.out.println(fact(-1));
}
}
medium
A. Memo map is not initialized properly
B. fact method should return long instead of int
C. Incorrect multiplication order in recursion
D. No base case for negative input causes infinite recursion
Solution
Step 1: Analyze base cases and input handling
Base case handles n == 0 only, but negative n causes no stop condition.
Step 2: Effect of negative input
Calling fact(-1) leads to infinite recursion because n never reaches 0 or memoized value.
Final Answer:
No base case for negative input causes infinite recursion -> Option D
Quick Check:
Missing negative base case = infinite recursion [OK]
Hint: Check base cases cover all input ranges [OK]
Common Mistakes:
Ignoring negative input handling
Assuming memo initialization error
Thinking multiplication order causes error
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 Java?
hard
A. Use a List<Integer> to store results; always calculate recursively without checking cache.
B. Use a Map to store results; check cache before recursive calls; store results after calculation.
C. Use an int array without initializing values; store results only after all recursion ends.
D. Use a Set<Integer> to store visited stairs; return size of set as result.
Solution
Step 1: Understand problem and memoization needs
We must store number of ways for each stair number to avoid repeated calculations.
Step 2: Evaluate memoization approaches
Use a Map to store results; check cache before recursive calls; store results after calculation. uses Map to store and check results before recursion, which is correct memoization practice.
Final Answer:
Use a Map to store results; check cache before recursive calls; store results after calculation. -> Option B
Quick Check:
Memo = Map with cache check and store [OK]
Hint: Check cache before recursion, store after calculation [OK]
Common Mistakes:
Not checking cache before recursive calls
Using List or Set incorrectly for memoization
Storing results too late or not at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.