Challenge - 5 Problems
Rod Cutting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Rod Cutting
What is the output of the following Java code that uses a simple recursive approach to solve the rod cutting problem for a rod of length 4?
DSA Java
public class RodCutting { static int cutRod(int[] price, int n) { if (n <= 0) return 0; int max_val = Integer.MIN_VALUE; for (int i = 1; i <= n; i++) { max_val = Math.max(max_val, price[i - 1] + cutRod(price, n - i)); } return max_val; } public static void main(String[] args) { int[] price = {1, 5, 8, 9}; int length = 4; System.out.println(cutRod(price, length)); } }
Attempts:
2 left
💡 Hint
Consider all possible cuts and sum their prices recursively.
✗ Incorrect
The maximum revenue for length 4 is obtained by cutting into lengths 2 and 2, each priced 5, total 10.
❓ Predict Output
intermediateOutput of Bottom-Up DP Rod Cutting
What is the output of this Java code using bottom-up dynamic programming for rod cutting with prices {2, 5, 7, 8} and rod length 4?
DSA Java
public class RodCuttingDP { static int cutRod(int[] price, int n) { int[] val = new int[n + 1]; val[0] = 0; for (int i = 1; i <= n; i++) { int max_val = Integer.MIN_VALUE; for (int j = 1; j <= i; j++) { max_val = Math.max(max_val, price[j - 1] + val[i - j]); } val[i] = max_val; } return val[n]; } public static void main(String[] args) { int[] price = {2, 5, 7, 8}; int length = 4; System.out.println(cutRod(price, length)); } }
Attempts:
2 left
💡 Hint
Use the DP array to build solutions from smaller lengths.
✗ Incorrect
The best cut is length 2 + length 2, prices 5 + 5 = 10.
🔧 Debug
advancedIdentify the Error in Memoized Rod Cutting
What error does this Java code produce when running the memoized rod cutting function?
DSA Java
public class RodCuttingMemo { static int cutRodMemo(int[] price, int n, int[] memo) { if (n <= 0) return 0; if (memo[n] >= 0) return memo[n]; int max_val = Integer.MIN_VALUE; for (int i = 1; i <= n; i++) { max_val = Math.max(max_val, price[i - 1] + cutRodMemo(price, n - i, memo)); } memo[n] = max_val; return max_val; } public static void main(String[] args) { int[] price = {1, 5, 8, 9}; int length = 4; int[] memo = new int[length + 1]; for (int i = 0; i <= length; i++) memo[i] = -1; System.out.println(cutRodMemo(price, length, memo)); } }
Attempts:
2 left
💡 Hint
Check the size of the memo array and the indices used.
✗ Incorrect
Memo array size is length 4 but accessed at index 4 causing out of bounds.
🧠 Conceptual
advancedTime Complexity of Rod Cutting Algorithms
What is the time complexity of the bottom-up dynamic programming solution for the rod cutting problem with rod length n?
Attempts:
2 left
💡 Hint
Consider the nested loops in the DP solution.
✗ Incorrect
The outer loop runs n times and inner loop runs up to n times, total O(n^2).
🚀 Application
expertMaximum Revenue and Cuts for Rod Length 5
Given prices {2, 5, 7, 8, 10} for rod lengths 1 to 5, what is the maximum revenue and the cuts that produce it for rod length 5 using bottom-up DP?
Attempts:
2 left
💡 Hint
Try all cut combinations and sum their prices.
✗ Incorrect
Cutting into lengths 2 and 3 yields 5 + 7 = 12, which is maximum.
