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 Go code that calculates Fibonacci numbers using memoization?
DSA Go
package main import "fmt" func fib(n int, memo map[int]int) int { if n <= 1 { return n } if val, ok := memo[n]; ok { return val } memo[n] = fib(n-1, memo) + fib(n-2, memo) return memo[n] } func main() { memo := make(map[int]int) fmt.Println(fib(5, memo)) }
Attempts:
2 left
💡 Hint
Remember that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5...
✗ Incorrect
The function calculates Fibonacci numbers with memoization. fib(5) returns 5 because the sequence is 0,1,1,2,3,5...
❓ Predict Output
intermediateOutput of Recursive Coin Change Count
What is the output of this Go code that counts ways to make change using recursion with overlapping subproblems?
DSA Go
package main import "fmt" func countWays(coins []int, m int, n int) int { if n == 0 { return 1 } if n < 0 || m <= 0 { return 0 } return countWays(coins, m-1, n) + countWays(coins, m, n-coins[m-1]) } func main() { coins := []int{1, 2, 3} fmt.Println(countWays(coins, len(coins), 4)) }
Attempts:
2 left
💡 Hint
Count all combinations of coins that sum to 4.
✗ Incorrect
The function counts all ways to make 4 using coins 1,2,3. The total ways are 4.
🔧 Debug
advancedIdentify the error in memoized Fibonacci
What error does this Go code produce when run, and why?
DSA Go
package main import "fmt" func fib(n int, memo map[int]int) int { if n <= 1 { return n } if val, ok := memo[n]; ok { return val } memo[n] = fib(n-1, memo) + fib(n-2, memo) return memo[n] } func main() { var memo map[int]int fmt.Println(fib(5, memo)) }
Attempts:
2 left
💡 Hint
Check how the map is initialized before use.
✗ Incorrect
The map memo is declared but not initialized, so writing to it causes a runtime panic.
🚀 Application
advancedOptimal Substructure in Longest Common Subsequence
Given two strings, which statement best describes the optimal substructure property used in the Longest Common Subsequence (LCS) problem?
Attempts:
2 left
💡 Hint
Think about how the problem breaks down into smaller subproblems.
✗ Incorrect
The LCS problem uses optimal substructure by relating the solution to smaller subproblems depending on character matches.
🧠 Conceptual
expertWhy Overlapping Subproblems Matter in Dynamic Programming
Which of the following best explains why overlapping subproblems are important for dynamic programming?
Attempts:
2 left
💡 Hint
Think about how dynamic programming improves efficiency.
✗ Incorrect
Dynamic programming stores results of subproblems to avoid repeated work, which is possible only if subproblems overlap.
