Challenge - 5 Problems
Dynamic Programming Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Fibonacci Without Memoization
What is the output of the following C# code that calculates Fibonacci numbers using simple recursion without memoization?
DSA C#
using System; class Program { static int Fib(int n) { if (n <= 1) return n; return Fib(n - 1) + Fib(n - 2); } static void Main() { Console.WriteLine(Fib(5)); } }
Attempts:
2 left
💡 Hint
Recall the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8...
✗ Incorrect
Fib(5) = Fib(4) + Fib(3) = 3 + 2 = 5. The code correctly computes the 5th Fibonacci number (0-based index).
❓ Predict Output
intermediateOutput of Recursive Fibonacci with Memoization
What is the output of this C# code that calculates Fibonacci numbers using recursion with 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(10)); } }
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
✗ Incorrect
Fib(10) = 55 in the Fibonacci sequence starting from 0. Memoization prevents redundant calls and stack overflow.
🧠 Conceptual
advancedWhy Does Simple Recursion Fail for Large Inputs?
Why does simple recursion without memoization often fail or become inefficient for problems like Fibonacci calculation?
Attempts:
2 left
💡 Hint
Think about how many times the same Fibonacci numbers are calculated.
✗ Incorrect
Simple recursion recalculates the same subproblems repeatedly, leading to exponential time and inefficiency.
🧠 Conceptual
advancedHow Does Dynamic Programming Improve Recursive Solutions?
How does dynamic programming improve the efficiency of recursive algorithms?
Attempts:
2 left
💡 Hint
Think about how storing answers helps.
✗ Incorrect
Dynamic programming stores answers to subproblems (memoization or tabulation) so they are computed once, improving efficiency.
🔧 Debug
expertIdentify the Reason for Stack Overflow in Recursive Code
Given the following C# code, what is the main reason it causes a StackOverflowException when called with Fib(1000)?
DSA C#
using System; class Program { static int Fib(int n) { if (n <= 1) return n; return Fib(n - 1) + Fib(n - 2); } static void Main() { Console.WriteLine(Fib(1000)); } }
Attempts:
2 left
💡 Hint
Consider how many recursive calls happen for large inputs without optimization.
✗ Incorrect
Without memoization, the recursion tree grows exponentially causing too many nested calls and stack overflow.
