What is memoization in the context of dynamic programming?
Memoization is a technique where we store the results of expensive function calls and reuse them when the same inputs occur again, avoiding repeated calculations.
Click to reveal answer
beginner
How does top-down dynamic programming differ from bottom-up dynamic programming?
Top-down DP solves the problem by breaking it down recursively and storing results (memoization), while bottom-up DP solves smaller subproblems first and builds up to the solution iteratively.
Click to reveal answer
beginner
In Java, what data structure is commonly used to store memoized results in top-down DP?
A common data structure is an array or a HashMap, depending on the problem's input size and type, to store computed results for subproblems.
Click to reveal answer
intermediate
Why is memoization important in recursive solutions for problems like Fibonacci numbers?
Without memoization, recursive calls repeat the same calculations many times, causing exponential time complexity. Memoization reduces this to linear time by saving and reusing results.
Click to reveal answer
beginner
What is a key step to implement memoization in a top-down DP approach?
Before computing a subproblem, check if its result is already stored. If yes, return it immediately; if no, compute, store, and then return the result.
Click to reveal answer
What does memoization primarily help to avoid in recursive algorithms?
AUsing extra memory
BRepeated calculations of the same subproblems
CWriting iterative code
DSorting input data
✗ Incorrect
Memoization stores results of subproblems to avoid recalculating them, improving efficiency.
In top-down DP, when do you store the result of a subproblem?
AAfter computing it for the first time
BBefore computing it
COnly if the result is zero
DNever, results are recalculated
✗ Incorrect
You store the result after computing it to reuse it later and avoid repeated work.
Which of these is a typical sign that memoization can improve a recursive solution?
AThe function has no base case
BThe function uses loops
CThe function returns a constant value
DThe same function calls repeat with the same inputs
✗ Incorrect
Repeated calls with the same inputs indicate overlapping subproblems, ideal for memoization.
What is the time complexity improvement when using memoization on Fibonacci calculation?
ANo improvement
BFrom linear to exponential
CFrom exponential to linear
DFrom quadratic to cubic
✗ Incorrect
Memoization reduces Fibonacci calculation from exponential time to linear time.
Which data structure is NOT commonly used for memoization in Java?
AHashSet
BHashMap
CLinkedList
DArray
✗ Incorrect
HashSet stores unique values but does not map inputs to results, so it is not used for memoization.
Explain how memoization works in a top-down dynamic programming approach with an example.
Think about how you save answers to smaller problems to use later.
You got /4 concepts.
Describe the difference between top-down memoization and bottom-up dynamic programming.
Consider how the problem is solved step-by-step in each approach.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of memoization in top-down dynamic programming?
easy
A. To increase the size of the input for better accuracy
B. To sort the input data before processing
C. To store results of subproblems to avoid repeated calculations
D. To convert recursive code into iterative code
Solution
Step 1: Understand memoization role
Memoization saves answers of smaller problems so they are not recalculated.
Step 2: Compare options
Only To store results of subproblems to avoid repeated calculations correctly describes storing subproblem results to avoid repeats.
Final Answer:
To store results of subproblems to avoid repeated calculations -> Option C
Quick Check:
Memoization = store subproblem results [OK]
Hint: Memoization means saving answers to reuse later [OK]
Common Mistakes:
Confusing memoization with sorting
Thinking memoization changes recursion to iteration
Believing memoization increases input size
2. Which of the following is the correct way to declare a memo array for Fibonacci numbers in Java?
easy
A. int memo[] = new int();
B. int memo = new int[n];
C. int[] memo = new int[];
D. int[] memo = new int[n+1];
Solution
Step 1: Check Java array declaration syntax
Correct syntax for array with size n+1 is: int[] memo = new int[n+1];
Step 2: Identify errors in other options
int memo = new int[n]; declares int, not array; C misses size; D uses wrong constructor syntax.
Final Answer:
int[] memo = new int[n+1]; -> Option D
Quick Check:
Array declaration = int[] memo = new int[size] [OK]
Hint: Use int[] memo = new int[n+1] for memo arrays [OK]
Common Mistakes:
Declaring memo as int instead of int[]
Not specifying array size when creating
Using parentheses instead of brackets for arrays
3. What will be the output of the following Java code using memoization for Fibonacci?
public class FibMemo {
static int[] memo;
public static int fib(int n) {
if (n <= 1) return n;
if (memo[n] != -1) return memo[n];
memo[n] = fib(n-1) + fib(n-2);
return memo[n];
}
public static void main(String[] args) {
int n = 5;
memo = new int[n+1];
for (int i = 0; i <= n; i++) memo[i] = -1;
System.out.println(fib(n));
}
}
medium
A. 5
B. 8
C. 3
D. Error due to uninitialized memo
Solution
Step 1: Understand Fibonacci calculation with memo
fib(5) = fib(4) + fib(3), recursively calculated and stored in memo.