Challenge - 5 Problems
Memoization Mastery
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 C# code that calculates Fibonacci numbers using memoization?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<int, int> memo = new Dictionary<int, int>(); static int Fib(int n) { if (n <= 1) return n; if (memo.ContainsKey(n)) return memo[n]; int result = Fib(n - 1) + Fib(n - 2); memo[n] = result; return result; } static void Main() { Console.WriteLine(Fib(6)); } }
Attempts:
2 left
💡 Hint
Remember that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13...
✗ Incorrect
Fib(6) = Fib(5) + Fib(4) = 5 + 3 = 8. Memoization stores intermediate results to avoid repeated calculations.
❓ Predict Output
intermediateMemoization Cache State After Calls
After calling Fib(5) using the memoized Fibonacci function below, what is the content of the memo dictionary?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<int, int> memo = new Dictionary<int, int>(); static int Fib(int n) { if (n <= 1) return n; if (memo.ContainsKey(n)) return memo[n]; int result = Fib(n - 1) + Fib(n - 2); memo[n] = result; return result; } static void Main() { Fib(5); foreach(var kvp in memo) { Console.WriteLine($"{kvp.Key}:{kvp.Value}"); } } }
Attempts:
2 left
💡 Hint
Memo stores results only for n > 1 after calculation.
✗ Incorrect
Fib(2)=1, Fib(3)=2, Fib(4)=3, Fib(5)=5 are stored. Fib(1) and Fib(0) are base cases and not stored.
🔧 Debug
advancedIdentify the Bug in Memoized Factorial
What error will the following C# code produce when calling Factorial(5)?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<int, int> memo = new Dictionary<int, int>(); static int Factorial(int n) { if (n == 0) return 1; if (memo.ContainsKey(n)) return memo[n]; int result = n * Factorial(n - 1); // Missing memoization store here return result; } static void Main() { Console.WriteLine(Factorial(5)); } }
Attempts:
2 left
💡 Hint
Check if memo dictionary is updated after calculation.
✗ Incorrect
Though memo is not updated, the recursion works correctly and returns 120 for Factorial(5). Memoization is not effective but no error occurs.
❓ Predict Output
advancedOutput of Memoized Climbing Stairs Problem
What is the output of the following C# code that uses memoization to count ways to climb stairs?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<int, int> memo = new Dictionary<int, int>(); static int ClimbStairs(int n) { if (n <= 1) return 1; if (memo.ContainsKey(n)) return memo[n]; int ways = ClimbStairs(n - 1) + ClimbStairs(n - 2); memo[n] = ways; return ways; } static void Main() { Console.WriteLine(ClimbStairs(4)); } }
Attempts:
2 left
💡 Hint
Number of ways to climb 4 steps with 1 or 2 steps at a time.
✗ Incorrect
Ways to climb 4 steps = ways(3) + ways(2) = 3 + 2 = 5.
🧠 Conceptual
expertMemoization Impact on Recursive Tree Size
Consider a naive recursive function without memoization that computes Fibonacci numbers. How does adding memoization affect the number of recursive calls?
Attempts:
2 left
💡 Hint
Think about repeated calculations in recursion.
✗ Incorrect
Memoization stores results of subproblems, so each is computed once, reducing calls from exponential to linear.
