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 for Fibonacci
What is the output of the following Java code that uses bottom-up dynamic programming to compute the 6th Fibonacci number?
DSA Java
public class FibDP { public static void main(String[] args) { int n = 6; int[] dp = new int[n + 1]; dp[0] = 0; dp[1] = 1; for (int i = 2; i <= n; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } System.out.println(dp[n]); } }
Attempts:
2 left
š” Hint
Remember Fibonacci sequence starts with 0, 1 and each next number is sum of previous two.
ā Incorrect
The Fibonacci sequence starting with 0,1 is: 0,1,1,2,3,5,8,... The 6th Fibonacci number (0-based) is 8.
ā Predict Output
intermediateOutput of Bottom-Up DP for Coin Change Ways
What is the output of this Java code that calculates the number of ways to make change for amount 5 using coins {1,2} with bottom-up DP?
DSA Java
public class CoinChange { public static void main(String[] args) { int amount = 5; int[] coins = {1, 2}; int[] dp = new int[amount + 1]; dp[0] = 1; for (int coin : coins) { for (int i = coin; i <= amount; i++) { dp[i] += dp[i - coin]; } } System.out.println(dp[amount]); } }
Attempts:
2 left
š” Hint
Count all combinations of coins that sum to 5.
ā Incorrect
Ways to make 5 with {1,2} are: 1+1+1+1+1, 1+1+1+2, 1+2+2 (order doesn't matter), total 3 ways.
š§ Debug
advancedIdentify the error in bottom-up DP for Longest Increasing Subsequence length
What error will this Java code produce when run? It tries to find the length of the longest increasing subsequence using bottom-up DP.
DSA Java
public class LIS { public static void main(String[] args) { int[] nums = {10, 9, 2, 5, 3, 7, 101, 18}; int n = nums.length; int[] dp = new int[n]; for (int i = 0; i < n; i++) { dp[i] = 1; } for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (nums[i] > nums[j]) { dp[i] = Math.max(dp[i], dp[j] + 1); } } } int max = 0; for (int length : dp) { max = Math.max(max, length); } System.out.println(max); } }
Attempts:
2 left
š” Hint
Check if the code initializes dp correctly and updates max length properly.
ā Incorrect
The code correctly computes the length of the longest increasing subsequence which is 4 for the input array.
ā Predict Output
advancedOutput of Bottom-Up DP for 0/1 Knapsack Maximum Value
What is the output of this Java code that uses bottom-up DP to find the maximum value for 0/1 knapsack with capacity 5 and items with weights {1,2,3} and values {6,10,12}?
DSA Java
public class Knapsack { public static void main(String[] args) { int[] weights = {1, 2, 3}; int[] values = {6, 10, 12}; int capacity = 5; int n = weights.length; int[][] dp = new int[n + 1][capacity + 1]; for (int i = 1; i <= n; i++) { for (int w = 1; w <= capacity; w++) { if (weights[i - 1] <= w) { dp[i][w] = Math.max(dp[i - 1][w], dp[i - 1][w - weights[i - 1]] + values[i - 1]); } else { dp[i][w] = dp[i - 1][w]; } } } System.out.println(dp[n][capacity]); } }
Attempts:
2 left
š” Hint
Try to pick items to maximize value without exceeding capacity 5.
ā Incorrect
Best combination is items with weights 2 and 3 (values 10 + 12 = 22).
š§ Conceptual
expertTime Complexity of Bottom-Up DP for Subset Sum Problem
What is the time complexity of the bottom-up dynamic programming solution for the subset sum problem where n is the number of elements and sum is the target sum?
Attempts:
2 left
š” Hint
Consider the DP table size and how many states are computed.
ā Incorrect
The DP table has n rows and sum+1 columns, so time complexity is O(n * sum).
