Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the DP array with zeros.
DSA Javascript
const dp = new Array(n + 1).fill([1]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using null or undefined causes errors when performing arithmetic later.
Filling with false is a boolean, not a number.
✗ Incorrect
We fill the dp array with 0 because in bottom-up DP, initial states usually start at zero.
2fill in blank
mediumComplete the code to start the loop from the first index for tabulation.
DSA Javascript
for (let i = [1]; i <= n; i++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 when the problem requires starting from 1.
Starting from n causes no iterations.
✗ Incorrect
In bottom-up DP, we usually start from 1 to build up solutions from smaller subproblems.
3fill in blank
hardFix the error in the code to update dp[i] using dp[i - 1].
DSA Javascript
dp[i] = dp[i - 1] [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction causes decreasing values incorrectly.
Multiplication or division is not suitable here.
✗ Incorrect
We add 1 to dp[i - 1] to build the solution incrementally in bottom-up DP.
4fill in blank
hardFill both blanks to complete the condition and update in the DP loop.
DSA Javascript
if (arr[i - 1] [1] target) { dp[i] = dp[i - 1] [2] dp[i - 1 - arr[i - 1]]; } else { dp[i] = dp[i - 1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of <= causes logic errors.
Using subtraction instead of addition in dp update.
✗ Incorrect
We check if current element is less or equal to target and add ways from previous states.
5fill in blank
hardFill all three blanks to complete the nested loop for 2D DP table filling.
DSA Javascript
for (let i = 1; i <= n; i++) { for (let j = 0; j <= target; j++) { if (arr[i - 1] [1] j) { dp[i][j] = dp[i - 1][j] [2] dp[i - 1][j [3] arr[i - 1]]; } else { dp[i][j] = dp[i - 1][j]; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of <= in condition.
Using subtraction instead of addition for dp update.
Using addition instead of subtraction inside index calculation.
✗ Incorrect
We check if element fits (<=), then add ways (plus) from excluding and including element (subtracting its value from j).
