Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the DP table for the 0 1 Knapsack problem.
DSA Go
dp := make([][]int, n+1) for i := 0; i <= n; i++ { dp[i] = make([]int, [1]+1) }
Attempts:
3 left
💡 Hint
Common Mistakes
Using the number of items 'n' instead of the weight capacity 'W' for the second dimension.
Using zero or a wrong variable for the size.
✗ Incorrect
The DP table has dimensions (n+1) x (W+1), where W is the maximum weight capacity.
2fill in blank
mediumComplete the code to check if the current item's weight fits in the knapsack capacity.
DSA Go
if weights[i-1] [1] w { // item can be included }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which checks if the item is too heavy.
Using '==' which is too restrictive.
✗ Incorrect
We check if the item's weight is less than or equal to the current capacity to decide if it can be included.
3fill in blank
hardFix the error in the code to update the DP table when including the current item.
DSA Go
dp[i][w] = max(dp[i-1][w], dp[i-1][w-[1]] + values[i-1])
Attempts:
3 left
💡 Hint
Common Mistakes
Using values instead of weights for the capacity subtraction.
Using weights[i] which is out of range.
✗ Incorrect
We subtract the weight of the current item (weights[i-1]) from the capacity to find the remaining capacity.
4fill in blank
hardFill both blanks to complete the loop that fills the DP table for the 0 1 Knapsack problem.
DSA Go
for i := 1; i <= [1]; i++ { for w := 1; w <= [2]; w++ { // fill dp[i][w] } }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the loops or using wrong variables like weights or values in loop limits.
✗ Incorrect
The outer loop runs over items (1 to n), and the inner loop runs over weights (1 to W).
5fill in blank
hardFill all three blanks to complete the max function used in the DP update.
DSA Go
func max(a, b int) int { if a [1] b { return [2] } return [3] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which reverses the logic.
Returning wrong variables.
✗ Incorrect
The function returns 'a' if a > b, else returns b.
