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 Go, what data structure is commonly used to implement memoization?
A map (dictionary) is commonly used in Go to store computed results with input parameters as keys for quick lookup.
Click to reveal answer
intermediate
What is a key 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 for problems like Fibonacci sequence calculation?
Because Fibonacci calculation has overlapping subproblems, memoization saves previously computed Fibonacci numbers, avoiding repeated work and drastically improving speed.
Click to reveal answer
What does memoization store to optimize recursion?
AFunction call stack
BInput parameters only
CResults of function calls
DRandom values
✗ Incorrect
Memoization stores the results of function calls to avoid recalculating them.
Which Go data structure is best for memoization?
AArray
BSlice
CStruct
DMap
✗ Incorrect
A map allows quick lookup of stored results by keys representing input parameters.
Memoization changes the time complexity of naive Fibonacci 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.
Memoization is a ____ approach in dynamic programming.
ABottom-up
BTop-down
CIterative
DGreedy
✗ Incorrect
Memoization uses recursion and caches results, which is a top-down approach.
What problem does memoization solve in recursion?
ARepeated calculations
BSyntax errors
CMemory leaks
DStack overflow
✗ Incorrect
Memoization avoids repeated calculations by storing results.
Explain how memoization optimizes a recursive function using a Go map.
Think about saving answers to subproblems and reusing them.
You got /4 concepts.
Describe the difference between memoization and tabulation in dynamic programming.
Consider how each approach solves subproblems.
You got /6 concepts.
Practice
(1/5)
1. What is the main purpose of memoization in recursive functions?
easy
A. To store results of expensive function calls and reuse them
B. To make the function run slower by adding extra steps
C. To remove recursion and use loops instead
D. To increase the memory usage without any benefit
Solution
Step 1: Understand what memoization does
Memoization saves the results of function calls so they don't need to be recalculated.
Step 2: Identify the benefit of memoization
By reusing saved results, the function runs faster and avoids repeated work.
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 work by caching results [OK]
Common Mistakes:
Thinking memoization slows down the function
Confusing memoization with removing recursion
Ignoring the memory benefit of caching
2. Which of the following Go code snippets correctly declares a memo map for memoization of integer keys and values?
easy
A. memo := new(map[int]int)
B. memo := make(map[int]int)
C. memo := make([]int, 10)
D. memo := map[string]int{}
Solution
Step 1: Identify correct map declaration syntax
In Go, make(map[int]int) creates a map with int keys and int values.
Step 2: Check other options
memo := map[string]int{} uses string keys, not int; memo := make([]int, 10) creates a slice, not a map; memo := new(map[int]int) creates a pointer to a map but does not initialize it.
Final Answer:
memo := make(map[int]int) -> Option B
Quick Check:
Correct map syntax = make(map[int]int) [OK]
Hint: Use make(map[int]int) to create int-to-int map [OK]
Common Mistakes:
Using slice instead of map for memo
Using wrong key type in map
Declaring pointer to map without initialization
3. What will be the output of the following Go code using memoization for Fibonacci numbers?
package main
import "fmt"
func fib(n int, memo map[int]int) int {
if n <= 1 {
return n
}
if val, ok := memo[n]; ok {
return val
}
memo[n] = fib(n-1, memo) + fib(n-2, memo)
return memo[n]
}
func main() {
memo := make(map[int]int)
fmt.Println(fib(5, memo))
}
medium
A. 8
B. Error
C. 3
D. 5
Solution
Step 1: Understand Fibonacci calculation with memo
fib(5) = fib(4) + fib(3), and memo stores intermediate results to avoid repeats.
4. Identify the error in this Go recursive function with memoization for factorial:
func fact(n int, memo map[int]int) int {
if n == 0 {
return 1
}
if val, ok := memo[n]; ok {
return val
}
memo[n] = n * fact(n-1, memo)
return memo[n]
}
func main() {
memo := make(map[int]int)
fmt.Println(fact(-1, memo))
}
medium
A. Compilation error due to map usage
B. No error, output is 1
C. Infinite recursion for negative input
D. Returns 0 for negative input
Solution
Step 1: Analyze base case and input
Base case is n == 0, but input is -1, so base case never reached.
Step 2: Understand recursion behavior
For n = -1, function calls fact(-2), then fact(-3), causing infinite recursion.
Final Answer:
Infinite recursion for negative input -> Option C
Quick Check:
Negative input causes infinite recursion [OK]
Hint: Check base case covers all inputs to avoid infinite recursion [OK]
Common Mistakes:
Assuming negative input returns 1
Ignoring infinite recursion possibility
Expecting compilation error incorrectly
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 using memoization in Go. Which approach below correctly applies memoization to avoid repeated calculations?
hard
A. Pass a map[int]int as a parameter to the function and store intermediate results in it
B. Use a slice of size n and fill it after recursion completes
C. Use a global variable to store results and update it inside the function
D. Call the recursive function twice without storing results
Solution
Step 1: Understand memoization with parameters
Passing a map as a parameter allows storing and reusing results during recursion.
Step 2: Evaluate options for memoization
Pass a map[int]int as a parameter to the function and store intermediate results in it correctly uses a map parameter; Use a global variable to store results and update it inside the function uses global state which is less safe; Use a slice of size n and fill it after recursion completes fills after recursion, not during; Call the recursive function twice without storing results repeats work without memo.
Final Answer:
Pass a map[int]int as a parameter to the function and store intermediate results in it -> Option A