Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the dp array with a large number.
DSA Typescript
const dp: number[] = new Array(amount + 1).fill([1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 will incorrectly suggest the amount can be formed without coins.
Using -1 is not suitable for minimum calculations.
Using amount does not represent an impossible state clearly.
✗ Incorrect
We use Infinity to represent that initially, no amount can be formed except 0.
2fill in blank
mediumComplete the code to set the base case for amount 0.
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 Infinity or -1 causes incorrect results.
Setting dp[0] to amount is incorrect.
✗ Incorrect
0 coins are needed to make amount 0.
3fill in blank
hardFix the error in the inner loop condition to avoid invalid indices.
DSA Typescript
for (let coin of coins) { if (i [1] coin) { dp[i] = Math.min(dp[i], dp[i - coin] + 1); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' or '>' causes index errors or skips valid cases.
✗ Incorrect
We must check if i is greater than or equal to coin to avoid negative indices.
4fill in blank
hardFill both blanks to complete the function that returns the minimum coins or -1 if not possible.
DSA Typescript
function coinChange(coins: number[], amount: number): number {
const dp: number[] = new Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (let coin of coins) {
if (i [1] coin) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] === [2] ? -1 : dp[amount];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' causes wrong index access.
Comparing dp[amount] to 0 instead of Infinity causes wrong return.
✗ Incorrect
We check if i >= coin to update dp[i]. If dp[amount] is still Infinity, return -1.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each coin to its count in the minimum coin combination.
DSA Typescript
function coinCount(coins: number[], amount: number): Record<number, number> {
const dp: number[] = new Array(amount + 1).fill(Infinity);
const count: number[] = new Array(amount + 1).fill(0);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (let coin of coins) {
if (i [1] coin && dp[i] > dp[i - coin] + 1) {
dp[i] = dp[i - coin] + 1;
count[i] = coin;
}
}
}
if (dp[amount] === Infinity) return {};
const result: Record<number, number> = {};
let current = amount;
while (current [3] 0) {
const c = count[current];
result[c] = (result[c] || 0) + 1;
current = current [2] c;
}
return result;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' or '>=' in the if condition causes wrong updates.
Using '+=' instead of '-=' in backtracking causes infinite loop.
✗ Incorrect
We check if i > coin to update dp and count. Then subtract coin from current to backtrack. '>=' is used in the while loop condition.