Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the maximum revenue array.
DSA Java
int[] maxRevenue = new int[[1] + 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using array size n causes index out of bounds when accessing maxRevenue[n].
Using n-1 misses the last length.
✗ Incorrect
We need an array of size n+1 to store maximum revenues for lengths from 0 to n.
2fill in blank
mediumComplete the code to set the base case revenue for rod length 0.
DSA Java
maxRevenue[0] = [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting base revenue to 1 or negative values causes incorrect results.
✗ Incorrect
Revenue for rod length 0 is zero because no cuts mean no revenue.
3fill in blank
hardFix the error in the loop that calculates maximum revenue for each rod length i.
DSA Java
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 - 1] + [1][i - j]); } maxRevenue[i] = maxVal; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using price instead of maxRevenue for the remaining length causes wrong calculation.
✗ Incorrect
We must add the revenue for the remaining rod length i-j, which is stored in maxRevenue array.
4fill in blank
hardFill both blanks to complete the method signature and return statement for the rod cutting solution.
DSA Java
public static int [1](int[] price, int [2]) { // method body return maxRevenue[n]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names or parameter names causes confusion.
✗ Incorrect
The method name is cutRod and the rod length parameter is commonly named n.
5fill in blank
hardFill all three blanks to complete the code that prints the maximum revenue for a rod of length n.
DSA Java
int[] price = {1, 5, 8, 9, 10, 17, 17, 20};
int [1] = price.length;
int maxProfit = [2](price, [3]);
System.out.println("Maximum Obtainable Value is " + maxProfit);Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names or method names causes errors.
✗ Incorrect
We define n as the length of price array, call cutRod with price and n, then print maxProfit.
