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 JavaScript code that solves the rod cutting problem using a simple recursive approach?
DSA Javascript
function cutRod(price, n) {
if (n <= 0) return 0;
let max_val = -Infinity;
for (let i = 1; i <= n; i++) {
max_val = Math.max(max_val, price[i - 1] + cutRod(price, n - i));
}
return max_val;
}
const prices = [1, 5, 8, 9, 10];
console.log(cutRod(prices, 5));Attempts:
2 left
💡 Hint
Think about all possible cuts and their combinations to maximize the price.
✗ Incorrect
The function tries all cuts from length 1 to n and recursively calculates the best price. For prices [1,5,8,9,10] and rod length 5, the best price is 13 by cutting into lengths 2 and 3 (5 + 8).
❓ Predict Output
intermediateOutput of Memoized Rod Cutting
What is the output of this JavaScript code that uses memoization to solve the rod cutting problem?
DSA Javascript
function memoizedCutRod(price, n, memo = {}) {
if (n <= 0) return 0;
if (memo[n]) return memo[n];
let max_val = -Infinity;
for (let i = 1; i <= n; i++) {
max_val = Math.max(max_val, price[i - 1] + memoizedCutRod(price, n - i, memo));
}
memo[n] = max_val;
return max_val;
}
const prices = [2, 5, 7, 8];
console.log(memoizedCutRod(prices, 4));Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
✗ Incorrect
The best price for rod length 4 with prices [2,5,7,8] is 10 by cutting into two pieces of length 2 (5 + 5).
🔧 Debug
advancedIdentify the Bug in Bottom-Up Rod Cutting
The following code is intended to solve the rod cutting problem using a bottom-up dynamic programming approach. What is the bug that causes incorrect output?
DSA Javascript
function bottomUpCutRod(price, n) {
let val = new Array(n + 1).fill(0);
for (let i = 1; i <= n; i++) {
let max_val = -Infinity;
for (let j = 1; j < i; j++) {
max_val = Math.max(max_val, price[j - 1] + val[i - j]);
}
val[i] = max_val;
}
return val[n];
}
const prices = [1, 5, 8, 9];
console.log(bottomUpCutRod(prices, 4));Attempts:
2 left
💡 Hint
Check the loop boundaries carefully.
✗ Incorrect
The inner loop runs from j=1 to j < i, so it misses the case when j equals i, which is needed to consider the whole rod without cutting.
🧠 Conceptual
advancedTime Complexity of Rod Cutting Algorithms
Which of the following correctly describes the time complexity of the naive recursive and the dynamic programming solutions for the rod cutting problem of length n?
Attempts:
2 left
💡 Hint
Consider how many subproblems are solved repeatedly in recursion.
✗ Incorrect
The naive recursive approach solves overlapping subproblems repeatedly, leading to exponential time. The dynamic programming approach solves each subproblem once, resulting in O(n^2) time.
🚀 Application
expertMaximum Revenue and Cuts for Rod Cutting
Given the prices array and rod length, which option correctly returns both the maximum revenue and the list of cuts to achieve it using bottom-up dynamic programming?
DSA Javascript
function extendedBottomUpCutRod(price, n) {
let val = new Array(n + 1).fill(0);
let cuts = new Array(n + 1).fill(0);
for (let i = 1; i <= n; i++) {
let max_val = -Infinity;
for (let j = 1; j <= i; j++) {
if (max_val < price[j - 1] + val[i - j]) {
max_val = price[j - 1] + val[i - j];
cuts[i] = j;
}
}
val[i] = max_val;
}
let resultCuts = [];
let length = n;
while (length > 0) {
resultCuts.push(cuts[length]);
length -= cuts[length];
}
return { maxRevenue: val[n], cuts: resultCuts };
}
const prices = [1, 5, 8, 9, 10];
console.log(extendedBottomUpCutRod(prices, 5));Attempts:
2 left
💡 Hint
Trace the cuts array to find the sequence of cuts that yield max revenue.
✗ Incorrect
The maximum revenue is 13 by cutting the rod into lengths 2 and 3. The cuts array stores the first cut length for each rod length, so the sequence is [2,3].
