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
intermediate2:00remaining
Output of Fibonacci Recursive Calls
What is the output of the following C code that prints Fibonacci numbers using recursion with overlapping subproblems?
DSA C
#include <stdio.h> int fib(int n) { if (n <= 1) return n; return fib(n-1) + fib(n-2); } int main() { for (int i = 0; i < 5; i++) { printf("%d ", fib(i)); } return 0; }
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts with 0 and 1 for fib(0) and fib(1).
✗ Incorrect
The Fibonacci sequence starts with 0 and 1. The recursive function returns fib(0)=0 and fib(1)=1, then sums previous two results. So the first five numbers are 0,1,1,2,3.
🧠 Conceptual
intermediate1:30remaining
Identifying Overlapping Subproblems
Which of the following problems best demonstrates the concept of overlapping subproblems in dynamic programming?
Attempts:
2 left
💡 Hint
Overlapping subproblems means the same smaller problem is solved multiple times.
✗ Incorrect
Naive recursive Fibonacci calculation solves the same fib(n) multiple times, showing overlapping subproblems. Factorial and quicksort do not reuse subproblems, and Dijkstra's algorithm is a greedy approach.
🔧 Debug
advanced2:30remaining
Debugging Memoization Implementation
What is the output of this C code with memoization for Fibonacci, and why?
DSA C
#include <stdio.h> int memo[10] = {0}; int fib(int n) { if (n <= 1) return n; if (memo[n] != 0) return memo[n]; memo[n] = fib(n-1) + fib(n-2); return memo[n]; } int main() { for (int i = 0; i < 7; i++) { printf("%d ", fib(i)); } return 0; }
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
✗ Incorrect
Memoization caches fib(n) results. Since fib(6) = fib(5) + fib(4) = 5 + 3 = 8, the output is the first 7 Fibonacci numbers starting from 0.
🚀 Application
advanced2:00remaining
Optimal Substructure in Coin Change Problem
Given unlimited coins of denominations 1, 3, and 4, what is the minimum number of coins needed to make amount 6 using optimal substructure?
Attempts:
2 left
💡 Hint
Try breaking amount 6 into smaller amounts and combine optimal solutions.
✗ Incorrect
Minimum coins for 6 can be found by checking 6-1=5, 6-3=3, 6-4=2. Minimum coins for 5 is 2 (4+1), for 3 is 1 (3), for 2 is 2 (1+1). So min is 1 + min(coins for 5,3,2) = 1 + 1 = 2. The minimum is 2 coins (e.g., 3+3).
🧠 Conceptual
expert2:30remaining
Recognizing Problems Without Optimal Substructure
Which problem does NOT exhibit optimal substructure, meaning its optimal solution cannot be constructed from optimal solutions of subproblems?
Attempts:
2 left
💡 Hint
Optimal substructure means combining optimal subproblem solutions gives global optimum.
✗ Incorrect
Longest simple path problem does not have optimal substructure because subpaths of the longest path are not necessarily longest paths themselves. Others have optimal substructure.