Challenge - 5 Problems
Coin Change Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Coin Change Ways Calculation
What is the output of the following TypeScript code that calculates the total number of ways to make change for amount 5 using coins [1, 2, 3]?
DSA Typescript
function countWays(coins: number[], amount: number): number {
const dp = Array(amount + 1).fill(0);
dp[0] = 1;
for (const coin of coins) {
for (let i = coin; i <= amount; i++) {
dp[i] += dp[i - coin];
}
}
return dp[amount];
}
console.log(countWays([1, 2, 3], 5));Attempts:
2 left
💡 Hint
Think about how many combinations of coins can sum up to 5 using 1, 2, and 3.
✗ Incorrect
The code uses dynamic programming to count ways. For amount 5 and coins [1,2,3], the total ways are 7.
🧠 Conceptual
intermediateUnderstanding DP Array Initialization
In the coin change total ways problem, why do we initialize dp[0] = 1 before starting the loops?
Attempts:
2 left
💡 Hint
Think about the base case for making amount zero.
✗ Incorrect
dp[0] = 1 means there is one way to make amount zero: by choosing no coins.
🔧 Debug
advancedIdentify the Error in Coin Change Code
What error will this TypeScript code produce when run?
DSA Typescript
function countWays(coins: number[], amount: number): number {
const dp = Array(amount + 1).fill(0);
dp[0] = 1;
for (let i = 0; i <= coins.length; i++) {
for (let j = coins[i]; j <= amount; j++) {
dp[j] += dp[j - coins[i]];
}
}
return dp[amount];
}
console.log(countWays([1, 2, 3], 5));Attempts:
2 left
💡 Hint
Check the loop bounds when accessing coins array.
✗ Incorrect
The loop runs i from 0 to coins.length inclusive, causing coins[i] to be undefined at last iteration.
❓ Predict Output
advancedOutput of Coin Change with Single Coin
What is the output of this code that calculates ways to make amount 10 using only coin 2?
DSA Typescript
function countWays(coins: number[], amount: number): number {
const dp = Array(amount + 1).fill(0);
dp[0] = 1;
for (const coin of coins) {
for (let i = coin; i <= amount; i++) {
dp[i] += dp[i - coin];
}
}
return dp[amount];
}
console.log(countWays([2], 10));Attempts:
2 left
💡 Hint
How many ways to make 10 using only 2s?
✗ Incorrect
Only one combination repeated 5 times: 2+2+2+2+2, so 1 way.
🧠 Conceptual
expertWhy Use Nested Loops in Coin Change DP?
Why does the coin change total ways solution use nested loops: outer loop over coins and inner loop over amounts?
Attempts:
2 left
💡 Hint
Think about how order of coins affects counting combinations.
✗ Incorrect
Outer loop over coins ensures combinations count without duplicates by processing coins one by one.
