0
0
DSA Typescriptprogramming~20 mins

Rod Cutting Problem in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rod Cutting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Recursive Rod Cutting
What is the output of the following TypeScript code that uses a recursive approach to solve the rod cutting problem for a rod of length 4?
DSA Typescript
function cutRod(prices: number[], n: number): number {
  if (n <= 0) return 0;
  let maxVal = -Infinity;
  for (let i = 1; i <= n; i++) {
    maxVal = Math.max(maxVal, prices[i - 1] + cutRod(prices, n - i));
  }
  return maxVal;
}

const prices = [1, 5, 8, 9];
const length = 4;
console.log(cutRod(prices, length));
A9
B10
C8
D5
Attempts:
2 left
💡 Hint
Think about all possible ways to cut the rod and sum prices.
Predict Output
intermediate
2:00remaining
Output of Memoized Rod Cutting
What is the output of this TypeScript code using memoization for rod cutting with prices [2, 5, 7, 8] and rod length 3?
DSA Typescript
function memoizedCutRod(prices: number[], n: number, memo: number[] = []): number {
  if (n <= 0) return 0;
  if (memo[n] !== undefined) return memo[n];
  let maxVal = -Infinity;
  for (let i = 1; i <= n; i++) {
    maxVal = Math.max(maxVal, prices[i - 1] + memoizedCutRod(prices, n - i, memo));
  }
  memo[n] = maxVal;
  return maxVal;
}

const prices = [2, 5, 7, 8];
const length = 3;
console.log(memoizedCutRod(prices, length));
A8
B14
C12
D7
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated work.
🔧 Debug
advanced
2:00remaining
Identify the Error in Bottom-Up Rod Cutting
What error does this TypeScript code produce when trying to solve the rod cutting problem using bottom-up dynamic programming?
DSA Typescript
function bottomUpCutRod(prices: number[], n: number): number {
  const dp = new Array(n + 1).fill(0);
  for (let j = 1; j <= n; j++) {
    let maxVal = -Infinity;
    for (let i = 1; i <= j; i++) {
      maxVal = Math.max(maxVal, prices[i] + dp[j - i]);
    }
    dp[j] = maxVal;
  }
  return dp[n];
}

const prices = [1, 5, 8, 9];
const length = 4;
console.log(bottomUpCutRod(prices, length));
ATypeError: Cannot read property '4' of undefined
BOutput: 9
CSyntaxError: Unexpected token
DOutput: 10
Attempts:
2 left
💡 Hint
Check array indexing carefully inside the inner loop.
Predict Output
advanced
2:00remaining
Output of Bottom-Up Rod Cutting Corrected
What is the output of this corrected bottom-up rod cutting code for prices [1, 5, 8, 9] and rod length 4?
DSA Typescript
function bottomUpCutRod(prices: number[], n: number): number {
  const dp = new Array(n + 1).fill(0);
  for (let j = 1; j <= n; j++) {
    let maxVal = -Infinity;
    for (let i = 1; i <= j; i++) {
      maxVal = Math.max(maxVal, prices[i - 1] + dp[j - i]);
    }
    dp[j] = maxVal;
  }
  return dp[n];
}

const prices = [1, 5, 8, 9];
const length = 4;
console.log(bottomUpCutRod(prices, length));
A5
B9
C8
D10
Attempts:
2 left
💡 Hint
Bottom-up builds solutions from smaller lengths up to n.
🧠 Conceptual
expert
2:00remaining
Time Complexity of Rod Cutting Algorithms
Which statement correctly describes the time complexity of the naive recursive, memoized, and bottom-up solutions for the rod cutting problem of length n?
ANaive recursive: exponential, Memoized: O(n^2), Bottom-up: O(n^2)
BNaive recursive: O(n), Memoized: O(n), Bottom-up: O(n)
CNaive recursive: O(n^2), Memoized: exponential, Bottom-up: O(n^2)
DNaive recursive: O(n^2), Memoized: O(n^2), Bottom-up: exponential
Attempts:
2 left
💡 Hint
Consider how many subproblems each approach solves and how often.