Challenge - 5 Problems
Master of DP vs Recursion vs Greedy
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Fibonacci with Memoization
What is the output of the following 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(6)); } }
Attempts:
2 left
💡 Hint
Recall the Fibonacci sequence: 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.
🧠 Conceptual
intermediateChoosing Greedy vs Dynamic Programming
Which problem characteristic best indicates that a greedy algorithm is suitable instead of dynamic programming?
Attempts:
2 left
💡 Hint
Greedy algorithms work well when making the best local choice leads to the best overall solution.
✗ Incorrect
Greedy algorithms are suitable when the problem has the greedy-choice property, meaning local optimal choices lead to a global optimum without needing to explore all possibilities.
❓ Predict Output
advancedOutput of Recursive vs DP Coin Change Count
What is the output of the following C# code that counts ways to make change using recursion and dynamic programming?
DSA C#
using System; class Program { static int CountWaysRecursive(int[] coins, int n, int amount) { if (amount == 0) return 1; if (amount < 0 || n <= 0) return 0; return CountWaysRecursive(coins, n - 1, amount) + CountWaysRecursive(coins, n, amount - coins[n - 1]); } static int CountWaysDP(int[] coins, int amount) { int[] dp = new int[amount + 1]; dp[0] = 1; foreach (var coin in coins) { for (int i = coin; i <= amount; i++) { dp[i] += dp[i - coin]; } } return dp[amount]; } static void Main() { int[] coins = {1, 2, 3}; int amount = 4; Console.WriteLine(CountWaysRecursive(coins, coins.Length, amount)); Console.WriteLine(CountWaysDP(coins, amount)); } }
Attempts:
2 left
💡 Hint
Count the number of ways to make 4 using coins 1, 2, 3.
✗ Incorrect
Both recursive and DP methods count 4 ways to make amount 4 with coins 1, 2, 3.
🧠 Conceptual
advancedWhy DP is Preferred Over Recursion Alone
Why is dynamic programming often preferred over plain recursion for problems with overlapping subproblems?
Attempts:
2 left
💡 Hint
Think about how DP stores results to save time.
✗ Incorrect
DP stores results of subproblems to avoid recalculating them, making it faster than plain recursion which may recompute the same results multiple times.
🚀 Application
expertChoosing the Right Tool for Job Scheduling Problem
You have a set of jobs with start and end times and profits. You want to schedule jobs to maximize total profit without overlapping. Which approach is best suited?
Attempts:
2 left
💡 Hint
Consider overlapping intervals and optimal substructure.
✗ Incorrect
The weighted job scheduling problem is best solved by dynamic programming after sorting jobs by end time, using stored results to find max profit without overlaps.
