Challenge - 5 Problems
Memoization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fibonacci with Memoization
What is the output of this Rust code that calculates Fibonacci numbers using memoization?
DSA Rust
use std::collections::HashMap;
fn fib(n: u32, memo: &mut HashMap<u32, u64>) -> u64 {
if n <= 1 {
return n as u64;
}
if let Some(&val) = memo.get(&n) {
return val;
}
let result = fib(n - 1, memo) + fib(n - 2, memo);
memo.insert(n, result);
result
}
fn main() {
let mut memo = HashMap::new();
let result = fib(10, &mut memo);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Recall that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...
✗ Incorrect
The 10th Fibonacci number (starting from 0) is 55. The memoization stores intermediate results to avoid repeated calculations.
❓ Predict Output
intermediateMemoized Factorial Calculation Output
What is printed by this Rust program that uses memoization to compute factorial?
DSA Rust
use std::collections::HashMap;
fn factorial(n: u32, memo: &mut HashMap<u32, u64>) -> u64 {
if n == 0 {
return 1;
}
if let Some(&val) = memo.get(&n) {
return val;
}
let result = n as u64 * factorial(n - 1, memo);
memo.insert(n, result);
result
}
fn main() {
let mut memo = HashMap::new();
let result = factorial(5, &mut memo);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Factorial of 5 is 5 * 4 * 3 * 2 * 1.
✗ Incorrect
The factorial of 5 is 120. Memoization caches intermediate factorial values to avoid repeated calculations.
🔧 Debug
advancedIdentify the Error in Memoized Fibonacci
This Rust code attempts to compute Fibonacci numbers with memoization but causes a runtime error. What is the error?
DSA Rust
use std::collections::HashMap;
fn fib(n: u32, memo: &mut HashMap<u32, u64>) -> u64 {
if n <= 1 {
return n as u64;
}
if memo.contains_key(&n) {
return memo[&n];
}
let result = fib(n - 1, memo) + fib(n - 2, memo);
memo.insert(n, result);
result
}
fn main() {
let mut memo = HashMap::new();
let result = fib(50, &mut memo);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Consider the recursion depth and Rust's default stack size.
✗ Incorrect
Even with memoization, the recursion depth for fib(50) is large and can cause stack overflow in Rust without tail recursion optimization.
🧠 Conceptual
advancedMemoization vs Tabulation
Which statement correctly describes the difference between memoization and tabulation in dynamic programming?
Attempts:
2 left
💡 Hint
Think about recursion and iteration approaches.
✗ Incorrect
Memoization is top-down recursion with caching results; tabulation is bottom-up iteration building solutions from smaller subproblems.
🚀 Application
expertMemoization to Optimize Grid Path Counting
Consider a grid of size 3x3. You can move only right or down from the top-left corner to the bottom-right corner. Using memoization, how many unique paths are there?
Attempts:
2 left
💡 Hint
Number of unique paths in an m x n grid is (m+n-2) choose (m-1).
✗ Incorrect
For a 3x3 grid, unique paths = C(4,2) = 6. Memoization helps avoid recomputing paths for subgrids.
