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 C# code that computes 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(5)); } }
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0, 1, 1, 2, 3, 5...
✗ Incorrect
Fib(5) = Fib(4) + Fib(3) = 3 + 2 = 5. The memo dictionary stores intermediate results to avoid repeated calculations.
❓ Predict Output
intermediateMemoized Climbing Stairs Output
What is the output of this 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 stairs with 1 or 2 steps at a time.
✗ Incorrect
ClimbStairs(4) = ClimbStairs(3) + ClimbStairs(2) = 3 + 2 = 5 ways.
🔧 Debug
advancedIdentify the error in memoized factorial code
What error does this C# code produce when run?
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); memo[n] = result; return result; } static void Main() { Console.WriteLine(Factorial(-1)); } }
Attempts:
2 left
💡 Hint
Check the base case and input value.
✗ Incorrect
Calling Factorial(-1) causes infinite recursion because base case only handles n == 0, leading to stack overflow.
🧠 Conceptual
advancedMemoization Cache Behavior
In a top-down memoized Fibonacci function, what happens when Fib(6) is called after Fib(5) has been computed and cached?
Attempts:
2 left
💡 Hint
Memoization stores all computed results for reuse.
✗ Incorrect
After Fib(5) is computed, both Fib(5) and Fib(4) are cached. Fib(6) uses both cached values without recomputation.
🚀 Application
expertLongest Increasing Subsequence Length with Memoization
Given the following C# code using memoization to find the length of the longest increasing subsequence (LIS), what is the output when input array is [10, 9, 2, 5, 3, 7, 101, 18]?
DSA C#
using System; using System.Collections.Generic; class Program { static int[] nums = {10, 9, 2, 5, 3, 7, 101, 18}; static Dictionary<int, int> memo = new Dictionary<int, int>(); static int LIS(int currentIndex) { if (memo.ContainsKey(currentIndex)) return memo[currentIndex]; int maxLength = 1; for (int next = currentIndex + 1; next < nums.Length; next++) { if (nums[next] > nums[currentIndex]) { int length = 1 + LIS(next); if (length > maxLength) maxLength = length; } } memo[currentIndex] = maxLength; return maxLength; } static void Main() { int maxLIS = 0; for (int i = 0; i < nums.Length; i++) { int length = LIS(i); if (length > maxLIS) maxLIS = length; } Console.WriteLine(maxLIS); } }
Attempts:
2 left
💡 Hint
Longest increasing subsequence length for this array is 4.
✗ Incorrect
The longest increasing subsequence is [2, 3, 7, 101] or [2, 5, 7, 101], length 4. The code computes the maximum length correctly using memoization.
