0
0
DSA Typescriptprogramming~10 mins

Coin Change Minimum Coins in DSA Typescript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aamount
BInfinity
C-1
D0
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.
2fill in blank
medium

Complete 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'
A0
Bamount
CInfinity
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0] to Infinity or -1 causes incorrect results.
Setting dp[0] to amount is incorrect.
3fill in blank
hard

Fix 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'
A>=
B>
C<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' or '>' causes index errors or skips valid cases.
4fill in blank
hard

Fill 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'
A>=
BInfinity
C<=
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>=' causes wrong index access.
Comparing dp[amount] to 0 instead of Infinity causes wrong return.
5fill in blank
hard

Fill 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'
A>=
B-=
C>
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' or '>=' in the if condition causes wrong updates.
Using '+=' instead of '-=' in backtracking causes infinite loop.