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 C++ code that uses a simple recursive approach to solve the rod cutting problem for a rod of length 4?
DSA C++
#include <iostream> #include <vector> #include <algorithm> #include <climits> using namespace std; int cutRod(const vector<int>& price, int n) { if (n <= 0) return 0; int max_val = INT_MIN; for (int i = 1; i <= n; i++) { max_val = max(max_val, price[i-1] + cutRod(price, n - i)); } return max_val; } int main() { vector<int> price = {1, 5, 8, 9}; int n = 4; cout << cutRod(price, n) << endl; return 0; }
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 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 bottom-up dynamic programming solution for rod cutting with prices {2, 5, 7, 8} and rod length 4?
DSA C++
#include <iostream> #include <vector> #include <climits> #include <algorithm> using namespace std; int cutRodDP(const vector<int>& price, int n) { vector<int> dp(n+1, 0); for (int i = 1; i <= n; i++) { int max_val = INT_MIN; for (int j = 1; j <= i; j++) { max_val = max(max_val, price[j-1] + dp[i-j]); } dp[i] = max_val; } return dp[n]; } int main() { vector<int> price = {2, 5, 7, 8}; int n = 4; cout << cutRodDP(price, n) << endl; return 0; }
Attempts:
2 left
💡 Hint
Check all possible cuts and use previously computed results.
✗ Incorrect
The best revenue is cutting into two pieces of length 2 each priced 5, total 10.
🔧 Debug
advancedIdentify the Bug in Memoized Rod Cutting
What error will this C++ code produce when running the memoized rod cutting function?
DSA C++
#include <iostream> #include <vector> #include <climits> #include <algorithm> using namespace std; int cutRodMemo(const vector<int>& price, int n, vector<int>& memo) { if (n <= 0) return 0; if (memo[n] != -1) return memo[n]; int max_val = INT_MIN; for (int i = 1; i <= n; i++) { max_val = max(max_val, price[i-1] + cutRodMemo(price, n - i, memo)); } memo[n] = max_val; return max_val; } int main() { vector<int> price = {1, 5, 8, 9}; int n = 4; vector<int> memo(n+1, -1); cout << cutRodMemo(price, n, memo) << endl; return 0; }
Attempts:
2 left
💡 Hint
Check the size of the memo vector and how it is accessed.
✗ Incorrect
The memo vector is of size n=4 but accessed at index n=4 which is out of range (valid indices 0-3).
🧠 Conceptual
advancedTime Complexity of Rod Cutting Algorithms
What is the time complexity of the naive recursive rod cutting solution for a rod of length n?
Attempts:
2 left
💡 Hint
Consider how many recursive calls are made without memoization.
✗ Incorrect
The naive recursive solution explores all possible cuts, leading to exponential calls.
🚀 Application
expertMaximum Revenue and Cuts for Rod Cutting
Given the prices {1, 5, 10, 9, 10} for rod lengths 1 to 5, what is the maximum revenue and the cuts that produce it for a rod of length 5 using bottom-up DP?
DSA C++
#include <iostream> #include <vector> #include <climits> #include <algorithm> #include <utility> using namespace std; pair<int, vector<int>> cutRodWithSolution(const vector<int>& price, int n) { vector<int> dp(n+1, 0); vector<int> cuts(n+1, 0); for (int i = 1; i <= n; i++) { int max_val = INT_MIN; for (int j = 1; j <= i; j++) { if (max_val < price[j-1] + dp[i-j]) { max_val = price[j-1] + dp[i-j]; cuts[i] = j; } } dp[i] = max_val; } vector<int> resultCuts; int length = n; while (length > 0) { resultCuts.push_back(cuts[length]); length -= cuts[length]; } return {dp[n], resultCuts}; } int main() { vector<int> price = {1, 5, 10, 9, 10}; int n = 5; auto result = cutRodWithSolution(price, n); cout << "Max Revenue: " << result.first << "\nCuts: "; for (int c : result.second) cout << c << " "; cout << endl; return 0; }
Attempts:
2 left
💡 Hint
Check all cut combinations and their total prices.
✗ Incorrect
The maximum revenue for length 5 is obtained by cutting into lengths 2 and 3, priced 5 and 10, total 15.
