Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with zeros.
DSA Typescript
const dp: number[] = new Array([1]).fill(0);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using coins.length instead of amount + 1 for dp size.
Forgetting to add 1 to amount for dp array length.
✗ Incorrect
We need dp array size to be amount + 1 to cover all amounts from 0 to amount.
2fill in blank
mediumComplete the code to set the base case for dp.
DSA Typescript
dp[0] = [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0] to 0 which means no ways to make zero amount.
Setting dp[0] to amount which is incorrect.
✗ Incorrect
There is exactly one way to make amount 0: use no coins.
3fill in blank
hardFix the error in the inner loop condition to avoid out-of-bound errors.
DSA Typescript
for (let j = coin; j [1] dp.length; j++) {
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes index out of bounds error.
Using '>' or '>=' causes the loop to never run or run incorrectly.
✗ Incorrect
The loop should run while j is less than dp.length to avoid index out of bounds.
4fill in blank
hardFill both blanks to correctly update dp array inside the loop.
DSA Typescript
dp[j] = dp[j] [1] dp[j [2] coin];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Using j + coin instead of j - coin inside dp index.
✗ Incorrect
We add the ways to make amount j and ways to make amount j - coin.
5fill in blank
hardFill all three blanks to complete the function that returns total ways to make amount.
DSA Typescript
function change(amount: number, coins: number[]): number {
const dp: number[] = new Array([1]).fill(0);
dp[0] = [2];
for (const coin of coins) {
for (let j = coin; j [3] dp.length; j++) {
dp[j] = dp[j] + dp[j - coin];
}
}
return dp[amount];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Wrong dp array size.
Incorrect base case value.
Wrong loop condition causing errors.
✗ Incorrect
dp array size is amount + 1, base case dp[0] = 1, loop runs while j < dp.length.