Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the maximum revenue to zero.
DSA C++
int maxRevenue = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing maxRevenue with 1 or INT_MAX which is incorrect for starting revenue.
✗ Incorrect
We start with maxRevenue as 0 because initially, no cuts have been made and revenue is zero.
2fill in blank
mediumComplete the code to loop through all possible first cut lengths.
DSA C++
for (int i = 1; i <= [1]; i++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using price or maxRevenue as loop limits which are unrelated to cut length.
✗ Incorrect
The loop runs from 1 to n, where n is the length of the rod, to consider all cut lengths.
3fill in blank
hardFix the error in the recursive call to compute revenue for the remaining rod length.
DSA C++
int revenue = price[i - 1] + cutRod(price, [1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using i - 1 or n + i which are incorrect for remaining length.
✗ Incorrect
After cutting length i, the remaining rod length is n - i, so the recursive call uses n - i.
4fill in blank
hardFill both blanks to update maxRevenue if current revenue is higher.
DSA C++
if (revenue [1] maxRevenue) { maxRevenue [2] revenue; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > or using += instead of assignment.
✗ Incorrect
We update maxRevenue only if revenue is greater, so use > and assign revenue to maxRevenue.
5fill in blank
hardFill all three blanks to complete the recursive rod cutting function.
DSA C++
int cutRod(int price[], int [1]) { if ([2] <= 0) return 0; int maxRevenue = 0; for (int i = 1; i <= [3]; i++) { int revenue = price[i - 1] + cutRod(price, n - i); if (revenue > maxRevenue) { maxRevenue = revenue; } } return maxRevenue; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently or wrong base case condition.
✗ Incorrect
The function parameter is n (rod length), base case checks if n <= 0, and loop runs i to n.
