Challenge - 5 Problems
Master of Overlapping Subproblems and Optimal Substructure
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
Recall that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5, 8, 13...
✗ Incorrect
The Fibonacci sequence with Fib(0)=0 and Fib(1)=1 produces Fib(6) = 8. The memo dictionary stores intermediate results to avoid repeated calculations.
🧠 Conceptual
intermediateIdentifying Optimal Substructure
Which of the following problems exhibits optimal substructure, meaning the optimal solution can be constructed from optimal solutions of its subproblems?
Attempts:
2 left
💡 Hint
Optimal substructure means the problem can be broken down into smaller problems whose solutions combine to solve the bigger problem optimally.
✗ Incorrect
Dijkstra's algorithm relies on optimal substructure: shortest paths to intermediate nodes are used to find shortest paths to further nodes.
🔧 Debug
advancedDebugging Memoization in Recursive Solution
The following C# code attempts to compute the minimum cost path in a grid using memoization but produces incorrect results. What is the main bug?
DSA C#
using System; using System.Collections.Generic; class Program { static int[,] grid = { {1, 3, 5}, {2, 1, 2}, {4, 3, 1} }; static Dictionary<(int,int), int> memo = new Dictionary<(int,int), int>(); static int MinCost(int r, int c) { if (r == 0 && c == 0) return grid[0,0]; if (r < 0 || c < 0) return int.MaxValue; if (memo.ContainsKey((r,c))) return memo[(r,c)]; int up = MinCost(r - 1, c); int left = MinCost(r, c - 1); int result = grid[r,c] + Math.Min(up, left); // Missing memoization store here return result; } static void Main() { Console.WriteLine(MinCost(2, 2)); } }
Attempts:
2 left
💡 Hint
Check if the memo dictionary is used to store results after computation.
✗ Incorrect
The code never stores computed results in memo, so memoization does not work and causes repeated calculations.
❓ Predict Output
advancedOutput of Longest Common Subsequence Length
What is the output of the following C# code that computes the length of the longest common subsequence (LCS) between two strings using recursion with memoization?
DSA C#
using System; using System.Collections.Generic; class Program { static Dictionary<(int,int), int> memo = new Dictionary<(int,int), int>(); static int LCS(string s1, string s2, int i, int j) { if (i == s1.Length || j == s2.Length) return 0; if (memo.ContainsKey((i,j))) return memo[(i,j)]; if (s1[i] == s2[j]) { memo[(i,j)] = 1 + LCS(s1, s2, i + 1, j + 1); } else { memo[(i,j)] = Math.Max(LCS(s1, s2, i + 1, j), LCS(s1, s2, i, j + 1)); } return memo[(i,j)]; } static void Main() { Console.WriteLine(LCS("AGGTAB", "GXTXAYB", 0, 0)); } }
Attempts:
2 left
💡 Hint
The longest common subsequence between "AGGTAB" and "GXTXAYB" is "GTAB" (length 4).
✗ Incorrect
The LCS length is 4 for these strings, computed by recursive memoized calls.
🧠 Conceptual
expertRecognizing Overlapping Subproblems in Algorithms
Which of the following algorithms does NOT exhibit overlapping subproblems, meaning it does not solve the same subproblems multiple times?
Attempts:
2 left
💡 Hint
Overlapping subproblems means the algorithm solves the same smaller problem multiple times.
✗ Incorrect
Merge sort divides the array into halves and sorts each half independently without recomputing the same subproblems multiple times.
