Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the maximum revenue array for rod lengths.
DSA C#
int[] maxRevenue = new int[[1] + 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using array size n instead of n+1 causes index out of range errors.
Using n-1 misses the last rod length.
✗ Incorrect
We need an array of size n+1 to store maximum revenues for all lengths from 0 to n.
2fill in blank
mediumComplete the code to iterate over all rod lengths from 1 to n.
DSA C#
for (int i = 1; i <= [1]; i++)
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of maxRevenue.Length - 1 causes confusion about array size.
Using maxRevenue.Length causes index out of range.
✗ Incorrect
The loop should run from 1 to maxRevenue.Length - 1 to cover all rod lengths.
3fill in blank
hardFix the error in the inner loop to iterate over possible first cut lengths.
DSA C#
for (int j = 1; j <= [1]; j++)
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of i causes incorrect iteration beyond current rod length.
Using maxRevenue.Length causes index errors.
✗ Incorrect
The inner loop must run from 1 to i, the current rod length being considered.
4fill in blank
hardFill both blanks to update maxRevenue[i] with the maximum revenue possible.
DSA C#
maxRevenue[i] = Math.Max(maxRevenue[i], [1][j - 1] + [2][i - j]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names like pricesList or price causes errors.
Mixing up indices leads to wrong revenue calculation.
✗ Incorrect
We use prices[j - 1] for the price of the first cut and maxRevenue[i - j] for the remaining length.
5fill in blank
hardFill all three blanks to return the maximum revenue for rod length n.
DSA C#
return [1][[2]]; // max revenue for length [3]
Attempts:
3 left
💡 Hint
Common Mistakes
Returning prices instead of maxRevenue returns wrong value.
Using n instead of maxRevenue.Length - 1 causes confusion about array size.
✗ Incorrect
maxRevenue[maxRevenue.Length - 1] holds the max revenue for rod length n.
