0
0
DSA Cprogramming~20 mins

Rod Cutting Problem in DSA C - 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 Rod Cutting Recursive Solution
What is the output of the following C code that solves the rod cutting problem using a simple recursive approach?
DSA C
#include <stdio.h>

int rodCutting(int price[], int n) {
    if (n <= 0) return 0;
    int max_val = -1;
    for (int i = 1; i <= n; i++) {
        int val = price[i - 1] + rodCutting(price, n - i);
        if (val > max_val) max_val = val;
    }
    return max_val;
}

int main() {
    int price[] = {1, 5, 8, 9};
    int n = 4;
    int result = rodCutting(price, n);
    printf("%d\n", result);
    return 0;
}
A8
B9
C10
D5
Attempts:
2 left
💡 Hint
Think about all possible cuts and their prices.
Predict Output
intermediate
2:00remaining
Output of Rod Cutting with Memoization
What is the output of this C code that uses memoization to solve the rod cutting problem?
DSA C
#include <stdio.h>
#include <string.h>

int memo[100];

int rodCuttingMemo(int price[], int n) {
    if (n <= 0) return 0;
    if (memo[n] != -1) return memo[n];
    int max_val = -1;
    for (int i = 1; i <= n; i++) {
        int val = price[i - 1] + rodCuttingMemo(price, n - i);
        if (val > max_val) max_val = val;
    }
    memo[n] = max_val;
    return max_val;
}

int main() {
    int price[] = {2, 5, 7, 8};
    int n = 4;
    memset(memo, -1, sizeof(memo));
    int result = rodCuttingMemo(price, n);
    printf("%d\n", result);
    return 0;
}
A14
B10
C16
D13
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated work.
🧠 Conceptual
advanced
1:30remaining
Time Complexity of Rod Cutting Recursive Solution
What is the time complexity of the naive recursive solution for the rod cutting problem with rod length n?
AO(n^2)
BO(n)
CO(n!)
DO(2^n)
Attempts:
2 left
💡 Hint
Consider how many recursive calls are made for each length.
Predict Output
advanced
2:00remaining
Output of Bottom-Up Rod Cutting DP
What is the output of this bottom-up dynamic programming solution for rod cutting?
DSA C
#include <stdio.h>

int rodCuttingDP(int price[], int n) {
    int dp[n+1];
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
        int max_val = -1;
        for (int j = 1; j <= i; j++) {
            int val = price[j - 1] + dp[i - j];
            if (val > max_val) max_val = val;
        }
        dp[i] = max_val;
    }
    return dp[n];
}

int main() {
    int price[] = {3, 5, 8, 9, 10};
    int n = 5;
    int result = rodCuttingDP(price, n);
    printf("%d\n", result);
    return 0;
}
A18
B17
C15
D16
Attempts:
2 left
💡 Hint
Bottom-up DP builds solutions from smaller lengths to larger.
🧠 Conceptual
expert
1:30remaining
Optimal Substructure in Rod Cutting Problem
Which statement best describes the optimal substructure property in the rod cutting problem?
AThe optimal solution for length n depends only on the optimal solutions of smaller lengths.
BThe problem cannot be divided into smaller subproblems.
CThe solution for length n is independent of solutions for smaller lengths.
DThe problem requires checking all permutations of cuts without reuse.
Attempts:
2 left
💡 Hint
Optimal substructure means solutions build on smaller solutions.