Challenge - 5 Problems
Bottom-Up DP Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Bottom-Up DP Fibonacci Implementation
What is the output of this Go code that uses bottom-up dynamic programming to find the 7th Fibonacci number?
DSA Go
package main import "fmt" func fib(n int) int { dp := make([]int, n+1) dp[0], dp[1] = 0, 1 for i := 2; i <= n; i++ { dp[i] = dp[i-1] + dp[i-2] } return dp[n] } func main() { fmt.Println(fib(7)) }
Attempts:
2 left
💡 Hint
Remember Fibonacci sequence starts 0,1,1,2,3,5,8,13,...
✗ Incorrect
The 7th Fibonacci number (0-based index) is 13. The code builds the sequence bottom-up and returns dp[7].
🧠 Conceptual
intermediateUnderstanding Table Initialization in Bottom-Up DP
In bottom-up dynamic programming, why do we initialize the dp table with base cases before the loop?
Attempts:
2 left
💡 Hint
Think about how each dp state depends on previous states.
✗ Incorrect
Base cases give known answers that the loop uses to build solutions for bigger problems.
❓ Predict Output
advancedOutput of Bottom-Up DP for Coin Change Ways
What is the output of this Go code that calculates the number of ways to make amount 5 using coins [1,2,5] with bottom-up DP?
DSA Go
package main import "fmt" func change(amount int, coins []int) int { dp := make([]int, amount+1) dp[0] = 1 for _, coin := range coins { for x := coin; x <= amount; x++ { dp[x] += dp[x-coin] } } return dp[amount] } func main() { fmt.Println(change(5, []int{1, 2, 5})) }
Attempts:
2 left
💡 Hint
Count combinations: {1+1+1+1+1}, {1+1+1+2}, {1+2+2}, {5}
✗ Incorrect
The code counts 4 ways to make 5: using coins 1,2,5 in combinations.
🔧 Debug
advancedIdentify the Error in Bottom-Up DP for Longest Increasing Subsequence
What error will this Go code produce when computing the length of the longest increasing subsequence using bottom-up DP?
DSA Go
package main import "fmt" func lengthOfLIS(nums []int) int { n := len(nums) dp := make([]int, n) maxLen := 1 for i := 0; i < n; i++ { dp[i] = 1 for j := 0; j < i; j++ { if nums[j] < nums[i] { dp[i] = max(dp[i], dp[j]+1) } } if dp[i] > maxLen { maxLen = dp[i] } } return maxLen } func max(a, b int) int { if a > b { return a } return b } func main() { fmt.Println(lengthOfLIS([]int{10,9,2,5,3,7,101,18})) }
Attempts:
2 left
💡 Hint
Check if all functions are defined and used correctly.
✗ Incorrect
The code correctly implements bottom-up DP for LIS and prints 4.
🚀 Application
expertMinimum Steps to One Using Bottom-Up DP
Given the code below, what is the value of dp[10] after running the bottom-up DP to find minimum steps to reduce 10 to 1 by subtracting 1, dividing by 2, or dividing by 3?
DSA Go
package main import "fmt" func minSteps(n int) int { dp := make([]int, n+1) dp[1] = 0 for i := 2; i <= n; i++ { dp[i] = dp[i-1] + 1 if i%2 == 0 && dp[i/2]+1 < dp[i] { dp[i] = dp[i/2] + 1 } if i%3 == 0 && dp[i/3]+1 < dp[i] { dp[i] = dp[i/3] + 1 } } return dp[n] } func main() { fmt.Println(minSteps(10)) }
Attempts:
2 left
💡 Hint
Trace steps: 10->9->3->1 (3 steps)
✗ Incorrect
The minimum steps to reduce 10 to 1 are 3 using the allowed operations.
