Challenge - 5 Problems
Coin Change Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of minimum coins needed for amount 11
What is the output of the following JavaScript code that calculates the minimum number of coins needed to make amount 11 using coins [1, 2, 5]?
DSA Javascript
function minCoins(coins, amount) {
const dp = Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (const coin of coins) {
if (coin <= i) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
}
console.log(minCoins([1, 2, 5], 11));Attempts:
2 left
💡 Hint
Think about the fewest coins that add up to 11 using 1, 2, and 5.
✗ Incorrect
The minimum coins to make 11 are 5 + 5 + 1, which is 3 coins.
❓ Predict Output
intermediateOutput when amount cannot be formed
What is the output of this code when trying to make amount 3 with coins [2, 4]?
DSA Javascript
function minCoins(coins, amount) {
const dp = Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (const coin of coins) {
if (coin <= i) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
}
console.log(minCoins([2, 4], 3));Attempts:
2 left
💡 Hint
Check if 3 can be formed by any combination of 2 and 4.
✗ Incorrect
3 cannot be formed using coins 2 and 4, so the function returns -1.
🧠 Conceptual
advancedWhy use dynamic programming for coin change minimum coins?
Why is dynamic programming a good approach to solve the minimum coin change problem?
Attempts:
2 left
💡 Hint
Think about how to avoid repeating the same calculations.
✗ Incorrect
Dynamic programming solves subproblems once and reuses their results, making it efficient for this problem.
❓ Predict Output
advancedOutput of minimum coins for amount 7 with coins [1, 3, 4]
What is the output of this code that finds minimum coins needed for amount 7 with coins [1, 3, 4]?
DSA Javascript
function minCoins(coins, amount) {
const dp = Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (let i = 1; i <= amount; i++) {
for (const coin of coins) {
if (coin <= i) {
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
}
console.log(minCoins([1, 3, 4], 7));Attempts:
2 left
💡 Hint
Try to find the fewest coins that sum to 7 using 1, 3, and 4.
✗ Incorrect
The minimum coins are 2 (4 + 3).
🚀 Application
expertMinimum coins for large amount with given coins
Given coins [1, 5, 10, 25] and amount 63, what is the minimum number of coins needed?
Attempts:
2 left
💡 Hint
Try to use the largest coins first to minimize count.
✗ Incorrect
63 can be made with two 25 coins, one 10 coin, and three 1 coins, total 6 coins.
