Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the maxRevenue slice with zeros.
DSA Go
maxRevenue := make([]int, [1])Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of n+1 causes index out of range errors.
Initializing with zero length slice.
✗ Incorrect
We need a slice of size n+1 to store maximum revenues for rod lengths from 0 to n.
2fill in blank
mediumComplete the code to update maxRevenue for length i by checking all cuts.
DSA Go
for j := 1; j <= i; j++ { maxRevenue[i] = max(maxRevenue[i], prices[j-1] + [1]) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using maxRevenue[j] instead of maxRevenue[i-j].
Adding prices[j] instead of prices[j-1].
✗ Incorrect
We add the price of the first cut (length j) to the best revenue for the remaining length (i-j).
3fill in blank
hardFix the error in the max function used to compare two integers.
DSA Go
func max(a, b int) int { if a [1] b { return a } return b }
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than operator returns the minimum instead of maximum.
Using equality operator causes incorrect logic.
✗ Incorrect
To return the maximum, we check if a is greater than b.
4fill in blank
hardFill both blanks to complete the loop that calculates maxRevenue for all lengths.
DSA Go
for [1] := 1; [2] <= n; [2]++ { // calculation }
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in initialization and condition causes errors.
Using j or k here is confusing since inner loops use j.
✗ Incorrect
The loop variable i is used to iterate from 1 to n for rod lengths.
5fill in blank
hardFill all three blanks to complete the rod cutting function signature and return statement.
DSA Go
func rodCutting([1] []int, [2] int) int { // implementation return [3] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter names causes confusion.
Returning wrong variable instead of maxRevenue[n].
✗ Incorrect
The function takes prices slice and rod length n, returns maxRevenue[n].
