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 Function
What is the output of this Go code that calculates Fibonacci numbers using recursion?
DSA Go
package main import "fmt" func fib(n int) int { if n <= 1 { return n } return fib(n-1) + fib(n-2) } func main() { fmt.Println(fib(5)) }
Attempts:
2 left
💡 Hint
Trace the recursive calls for fib(5).
✗ Incorrect
The recursive Fibonacci function returns the sum of the two previous Fibonacci numbers. fib(5) = fib(4) + fib(3) = 3 + 2 = 5.
❓ Predict Output
intermediateOutput of Greedy Coin Change Algorithm
What is the output of this Go code that uses a greedy approach to find minimum coins for amount 27 with coins [10, 5, 1]?
DSA Go
package main import "fmt" func minCoins(coins []int, amount int) int { count := 0 for _, coin := range coins { count += amount / coin amount %= coin } return count } func main() { coins := []int{10, 5, 1} fmt.Println(minCoins(coins, 27)) }
Attempts:
2 left
💡 Hint
Divide amount by largest coin first, then remainder by next coin.
✗ Incorrect
27/10=2 coins, remainder 7; 7/5=1 coin, remainder 2; 2/1=2 coins; total 2+1+2=5 coins.
❓ Predict Output
advancedOutput of DP Fibonacci with Memoization
What is the output of this Go code that calculates Fibonacci numbers using dynamic programming with memoization for fib(7)?
DSA Go
package main import "fmt" func fibDP(n int, memo map[int]int) int { if n <= 1 { return n } if val, ok := memo[n]; ok { return val } memo[n] = fibDP(n-1, memo) + fibDP(n-2, memo) return memo[n] } func main() { memo := make(map[int]int) fmt.Println(fibDP(7, memo)) }
Attempts:
2 left
💡 Hint
Use memoization to avoid repeated calculations.
✗ Incorrect
fib(7) = fib(6) + fib(5) = 8 + 5 = 13.
🧠 Conceptual
advancedChoosing the Right Tool for the Job
Which approach is best suited for problems where the solution can be built from solutions of overlapping subproblems and optimal substructure?
Attempts:
2 left
💡 Hint
Think about reusing solutions to subproblems.
✗ Incorrect
Dynamic programming is ideal when subproblems overlap and optimal substructure exists, allowing reuse of solutions.
🔧 Debug
expertIdentify the Error in Greedy Algorithm for Coin Change
This Go code tries to find minimum coins for amount 6 with coins [4, 3, 1] using a greedy approach. What error will it produce or what is the output?
DSA Go
package main import "fmt" func minCoinsGreedy(coins []int, amount int) int { count := 0 for _, coin := range coins { count += amount / coin amount %= coin } if amount != 0 { return -1 } return count } func main() { coins := []int{4, 3, 1} fmt.Println(minCoinsGreedy(coins, 6)) }
Attempts:
2 left
💡 Hint
Greedy may fail if coin denominations are not canonical.
✗ Incorrect
The greedy approach picks 4 once (remainder 2), then 3 zero times, then 1 twice; total 3 coins. But minimum coins is 2 (3+3). The code returns 3, which is suboptimal as greedy fails for these denominations.
