Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with zeros.
DSA C
int dp[[1] + 1]; for (int i = 0; i <= W; i++) { dp[i] = 0; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dp array size as W instead of W + 1
Not initializing dp array properly
✗ Incorrect
The dp array size should be W + 1 to cover all weights from 0 to W.
2fill in blank
mediumComplete the code to update dp[i] inside the loop for unbounded knapsack.
DSA C
for (int i = 0; i <= W; i++) { for (int j = 0; j < n; j++) { if (wt[j] <= i) { dp[i] = [1](dp[i], dp[i - wt[j]] + val[j]); } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using min instead of max
Using sum or abs which are incorrect here
✗ Incorrect
We use max to keep the maximum value achievable for weight i.
3fill in blank
hardFix the error in the loop condition to avoid out-of-bound access.
DSA C
for (int j = 0; j < [1]; j++) { // loop body }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using W instead of n in inner loop
Using array names as loop limits
✗ Incorrect
The inner loop must run over the number of items n, not weight W or arrays dp or val.
4fill in blank
hardFill both blanks to complete the function signature and return statement.
DSA C
int unboundedKnapsack(int [1], int [2], int val[], int wt[]) { // function body return dp[W]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping order of n and W
Using unclear parameter names
✗ Incorrect
The function takes number of items n and max weight W as parameters.
5fill in blank
hardFill all three blanks to complete the main loop and dp update for unbounded knapsack.
DSA C
for (int i = [1]; i <= [2]; i++) { for (int j = 0; j < [3]; j++) { if (wt[j] <= i) { dp[i] = max(dp[i], dp[i - wt[j]] + val[j]); } } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting i from 1 instead of 0
Using wrong loop limits for j
✗ Incorrect
The outer loop runs from 0 to W (max weight), inner loop runs over n items.