WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Dynamic Programming · Intermediate · question 23 of 100

Implement the Rod Cutting problem using dynamic programming.?

📕 Buy this interview preparation book: 100 Dynamic Programming questions & answers — PDF + EPUB for $5

The Rod Cutting problem is a classic optimization problem in which we are given a rod of length N and a price list that contains the prices of all possible rod lengths up to N. Our task is to cut the rod into smaller pieces in such a way that we can maximize our profit.

Here’s an optimal substructure for the problem: suppose we know the optimal solution for a rod of length k, then we can easily compute the optimal solution for a rod of length N (N > k) by just considering every possible cut of length k and adding the price of that cut with the optimal solution for a rod of length (N-k).

Let’s use dynamic programming to solve the problem step by step:

1. Define the problem in terms of smaller subproblems:

For a given rod of length i, let’s say we are given an array ’price’ where price[j] represents the price for a rod of length j. We want to find the maximum revenue we can obtain by cutting up the rod and selling the pieces.

2. Define the optimal value function:

Let’s define R[i] as the maximum revenue we can obtain by cutting a rod of length i. Our task is to calculate R[N], which is the maximum revenue we can obtain by cutting a rod of length N.

3. Define the base case:

We want to start with a rod of length 0, so R[0] = 0.

4. Define the recursive function:

We can calculate R[i] for all values of i in the following way:

int[] price = {0, 1, 5, 8, 9, 10, 17, 17, 20};

int rodCutting(int N, int[] price) {
    int[] R = new int[N+1];
    R[0] = 0;

    for(int i=1; i<=N; i++) {
        int maxVal = Integer.MIN_VALUE;
        for(int j=1; j<=i; j++) {
            maxVal = Math.max(maxVal, price[j]+R[i-j]);
        }
        R[i] = maxVal;
    }

    return R[N];
}

System.out.println(rodCutting(8, price)); // Output: 22

In the above code, we are iterating through all possible cut lengths j for a rod of length i and calculating the maximum revenue we can obtain by using the optimal solution for a rod of length (i-j) and adding the revenue obtained by cutting a rod of length j.

5. Define the solution:

The solution to the problem is stored in R[N], which is the maximum revenue we can obtain by cutting a rod of length N.

In the above example, the optimal solution for a rod of length 8 is obtained by cutting it into two pieces of length 2 and 6, which gives us a revenue of 1+17 = 18 for the first piece and a revenue of 17+5 = 22 for the second. Therefore, the maximum revenue we can obtain is 22.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Dynamic Programming interview — then scores it.
📞 Practice Dynamic Programming — free 15 min
📕 Buy this interview preparation book: 100 Dynamic Programming questions & answers — PDF + EPUB for $5

All 100 Dynamic Programming questions · All topics