Challenge - 5 Problems
Rod Cutting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Rod Cutting Maximum Revenue Calculation
What is the output of the following Go code that calculates the maximum revenue for a rod of length 4 with given prices?
DSA Go
package main import "fmt" func rodCutting(prices []int, n int) int { if n == 0 { return 0 } maxVal := -1 for i := 1; i <= n; i++ { val := prices[i-1] + rodCutting(prices, n-i) if val > maxVal { maxVal = val } } return maxVal } func main() { prices := []int{1, 5, 8, 9} length := 4 fmt.Println(rodCutting(prices, length)) }
Attempts:
2 left
💡 Hint
Think about all possible ways to cut the rod and sum the prices.
✗ Incorrect
The maximum revenue for length 4 is 10, achieved by cutting the rod into two pieces of length 2 each (5 + 5).
❓ Predict Output
intermediateOutput of Bottom-Up Rod Cutting Implementation
What is the output of this Go code that uses bottom-up dynamic programming to find maximum revenue for rod length 5?
DSA Go
package main import "fmt" func rodCutting(prices []int, n int) int { dp := make([]int, n+1) for i := 1; i <= n; i++ { maxVal := -1 for j := 1; j <= i; j++ { val := prices[j-1] + dp[i-j] if val > maxVal { maxVal = val } } dp[i] = maxVal } return dp[n] } func main() { prices := []int{2, 5, 7, 8, 10} length := 5 fmt.Println(rodCutting(prices, length)) }
Attempts:
2 left
💡 Hint
Check all possible cuts and use previously computed results.
✗ Incorrect
The best revenue for length 5 is 12, achieved by cutting into lengths 2 and 3 (5 + 7).
🔧 Debug
advancedIdentify the Error in Recursive Rod Cutting Code
What error does the following Go code produce when run?
DSA Go
package main import "fmt" func rodCutting(prices []int, n int) int { if n == 0 { return 0 } maxVal := 0 for i := 1; i <= n; i++ { val := prices[i] + rodCutting(prices, n-i) if val > maxVal { maxVal = val } } return maxVal } func main() { prices := []int{1, 5, 8, 9} length := 4 fmt.Println(rodCutting(prices, length)) }
Attempts:
2 left
💡 Hint
Check array indexing carefully inside the loop.
✗ Incorrect
The code accesses prices[i] where i can be equal to n, causing index out of range error.
🧠 Conceptual
advancedTime Complexity of Rod Cutting Recursive Solution
What is the time complexity of the naive recursive rod cutting solution without memoization for rod length n?
Attempts:
2 left
💡 Hint
Consider how many recursive calls are made for each length.
✗ Incorrect
The naive recursive solution explores all combinations, leading to exponential time O(2^n).
🚀 Application
expertMaximum Revenue and Cuts for Rod Cutting
Given prices for rod lengths and a rod length of 7, which option correctly shows the maximum revenue and the cuts to achieve it?
DSA Go
prices := []int{1, 5, 8, 9, 10, 17, 17} length := 7
Attempts:
2 left
💡 Hint
Try all cut combinations and sum their prices.
✗ Incorrect
Cutting into lengths 2, 2, 3 yields 5 + 5 + 8 = 18 revenue, which is maximum.
