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

Dynamic Programming · Advanced · question 55 of 100

Implement the solution to the Cutting a Rod with Maximum Product problem using dynamic programming.?

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

The Cutting a Rod with Maximum Product problem is to find the maximum product we can get by cutting a rod of length n into smaller pieces of integral length and selling them. Each length has a corresponding price value. We want to find the best way to cut the rod so that we get the maximum product by multiplying the prices of the pieces.

Here’s the dynamic programming solution to the problem:

1. Define the state: Let dp[i] be the maximum product we can get by cutting a rod of length i.

2. Define the base case: dp[0] = 1 (if the length of the rod is 0, the maximum product we can get is 1, since we can sell nothing and get nothing.)

3. Define the transition function: For each rod length i, we need to consider all possible cuts from 1 to i-1. For each cut j, we can split the rod into two pieces - one of length j and the other of length i-j. The maximum product we can get from this split is the product of the maximum product we can get from the pieces of length j and i-j, respectively. We want to find the best cut j that gives us the maximum product, and assign that value to dp[i]. In other words:

    dp[i] = max(dp[i], max(j * (i-j), dp[j] * dp[i-j]))

4. Return dp[n] as the answer: dp[n] will contain the maximum product we can get by cutting a rod of length n into smaller pieces.

Here’s the Java code for the solution:

public int maxProduct(int[] prices, int n) {
    int[] dp = new int[n+1];
    dp[0] = 1;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < i; j++) { // check all possible cuts
            dp[i] = Math.max(dp[i], Math.max(j * (i-j), dp[j] * dp[i-j]));
        }

        // check if selling the rod as a single piece is more profitable
        dp[i] = Math.max(dp[i], prices[i-1]);
    }

    return dp[n];
}

In this code, ‘prices‘ is an array containing the prices of different lengths of the rod, and ‘n‘ is the length of the rod we want to cut. We initialize ‘dp[0]‘ to 1, since if the length of the rod is 0, we can’t sell anything and still get some product.

We then loop over all possible rod lengths from 1 to ‘n‘. For each rod length ‘i‘, we loop over all possible cuts ‘j‘ from 1 to ‘i-1‘. For each cut, we calculate the maximum product we can get by multiplying the maximum products of the pieces of length ‘j‘ and ‘i-j‘, and taking the maximum of that value and the current value of ‘dp[i]‘.

Finally, we also check if selling the rod as a single piece (i.e., not cutting it at all) yields a higher profit than any of the cuts. We take the maximum of all these values and return it as the result.

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