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 C++ code that calculates the total number of ways to make change for amount 5 using coins {1, 2, 3}?
DSA C++
#include <iostream> int countWays(int coins[], int n, int amount) { int dp[amount+1] = {0}; dp[0] = 1; for (int i = 0; i < n; i++) { for (int j = coins[i]; j <= amount; j++) { dp[j] += dp[j - coins[i]]; } } return dp[amount]; } int main() { int coins[] = {1, 2, 3}; int amount = 5; int ways = countWays(coins, 3, amount); std::cout << ways << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Think about how many combinations of coins 1, 2, and 3 can sum up to 5.
✗ Incorrect
The code uses dynamic programming to count the number of combinations (order doesn't matter) to make change. For amount 5 with coins {1, 2, 3}, the total ways are 5.
❓ Predict Output
intermediateOutput of Coin Change with Single Coin
What is the output of this code that calculates the number of ways to make change for amount 4 using only coin {2}?
DSA C++
#include <iostream> int countWays(int coins[], int n, int amount) { int dp[amount+1] = {0}; dp[0] = 1; for (int i = 0; i < n; i++) { for (int j = coins[i]; j <= amount; j++) { dp[j] += dp[j - coins[i]]; } } return dp[amount]; } int main() { int coins[] = {2}; int amount = 4; int ways = countWays(coins, 1, amount); std::cout << ways << std::endl; return 0; }
Attempts:
2 left
💡 Hint
How many ways can you make 4 using only 2s?
✗ Incorrect
Using only coin 2, amount 4 can be made in exactly one combination: {2,2}.
🔧 Debug
advancedIdentify the Error in Coin Change Code
What error does the following code produce when trying to calculate the number of ways to make change for amount 3 with coins {1, 2}?
DSA C++
#include <iostream> int countWays(int coins[], int n, int amount) { int dp[amount]; dp[0] = 1; for (int i = 0; i < n; i++) { for (int j = coins[i]; j <= amount; j++) { dp[j] += dp[j - coins[i]]; } } return dp[amount]; } int main() { int coins[] = {1, 2}; int amount = 3; int ways = countWays(coins, 2, amount); std::cout << ways << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the size of dp array and indexing.
✗ Incorrect
The dp array is declared with size amount (3), but code accesses dp[3], which is out of bounds causing segmentation fault.
❓ Predict Output
advancedOutput of Coin Change with Zero Amount
What is the output of this code that calculates the number of ways to make change for amount 0 with coins {1, 2, 3}?
DSA C++
#include <iostream> int countWays(int coins[], int n, int amount) { int dp[amount+1] = {0}; dp[0] = 1; for (int i = 0; i < n; i++) { for (int j = coins[i]; j <= amount; j++) { dp[j] += dp[j - coins[i]]; } } return dp[amount]; } int main() { int coins[] = {1, 2, 3}; int amount = 0; int ways = countWays(coins, 3, amount); std::cout << ways << std::endl; return 0; }
Attempts:
2 left
💡 Hint
How many ways to make amount zero?
✗ Incorrect
There is exactly one way to make amount zero: use no coins.
❓ Predict Output
expertOutput of Coin Change with Large Amount and Multiple Coins
What is the output of this code that calculates the number of ways to make change for amount 10 using coins {2, 5, 3, 6}?
DSA C++
#include <iostream> int countWays(int coins[], int n, int amount) { int dp[amount+1] = {0}; dp[0] = 1; for (int i = 0; i < n; i++) { for (int j = coins[i]; j <= amount; j++) { dp[j] += dp[j - coins[i]]; } } return dp[amount]; } int main() { int coins[] = {2, 5, 3, 6}; int amount = 10; int ways = countWays(coins, 4, amount); std::cout << ways << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Count all combinations of coins {2,3,5,6} that sum to 10.
✗ Incorrect
The code counts all combinations without order. For amount 10 and coins {2,5,3,6}, total ways are 5.
