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 the following Rust code that calculates Fibonacci numbers using top-down memoization?
DSA Rust
use std::collections::HashMap;
fn fib(n: usize, memo: &mut HashMap<usize, usize>) -> usize {
if n <= 1 {
return n;
}
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(6, &mut memo);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13...
✗ Incorrect
The Fibonacci sequence starts with 0 and 1. fib(6) = fib(5) + fib(4) = 5 + 3 = 8.
❓ Predict Output
intermediateOutput of Climbing Stairs Problem with Memoization
What is the output of this Rust code that uses top-down memoization to count ways to climb stairs?
DSA Rust
use std::collections::HashMap;
fn climb_stairs(n: usize, memo: &mut HashMap<usize, usize>) -> usize {
if n <= 1 {
return 1;
}
if let Some(&val) = memo.get(&n) {
return val;
}
let ways = climb_stairs(n - 1, memo) + climb_stairs(n - 2, memo);
memo.insert(n, ways);
ways
}
fn main() {
let mut memo = HashMap::new();
let result = climb_stairs(4, &mut memo);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Number of ways to climb 4 stairs with 1 or 2 steps at a time.
✗ Incorrect
Ways to climb 4 stairs: 1+1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2 = 5 ways.
🔧 Debug
advancedIdentify the Error in Memoized Factorial Code
What error will this Rust code produce when run?
DSA Rust
use std::collections::HashMap;
fn factorial(n: usize, memo: &mut HashMap<usize, usize>) -> usize {
if n == 0 {
return 1;
}
if let Some(&val) = memo.get(&n) {
return val;
}
let result = n * factorial(n - 1, memo);
memo.insert(n, result);
result
}
fn main() {
let mut memo = HashMap::new();
let result = factorial(20, &mut memo);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Check the data type used for factorial result and its max value.
✗ Incorrect
usize on 64-bit systems is u64, which can hold 20! = 2432902008176640000 without overflow.
🧠 Conceptual
advancedMemoization Cache Behavior
In top-down memoization, what happens if the memo cache is not used properly?
Attempts:
2 left
💡 Hint
Memoization avoids repeated work by caching results.
✗ Incorrect
Without memoization cache, the function recalculates the same subproblems multiple times, slowing down the algorithm.
🚀 Application
expertMemoization for Unique Paths in Grid
Given this Rust code for counting unique paths in a grid using memoization, what is the output?
DSA Rust
use std::collections::HashMap;
fn unique_paths(m: usize, n: usize, memo: &mut HashMap<(usize, usize), usize>) -> usize {
if m == 1 || n == 1 {
return 1;
}
if let Some(&val) = memo.get(&(m, n)) {
return val;
}
let paths = unique_paths(m - 1, n, memo) + unique_paths(m, n - 1, memo);
memo.insert((m, n), paths);
paths
}
fn main() {
let mut memo = HashMap::new();
let result = unique_paths(3, 3, &mut memo);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Number of ways to move right or down in a 3x3 grid.
✗ Incorrect
Unique paths in 3x3 grid = 6 (combinations of moves).
