Challenge - 5 Problems
Coin Change Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output 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 <stdio.h> int countWays(int coins[], int n, int amount) { int dp[amount+1]; for (int i = 0; i <= amount; i++) dp[i] = 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 n = 3; int amount = 5; printf("%d\n", countWays(coins, n, amount)); return 0; }
Attempts:
2 left
💡 Hint
Think about how many combinations of coins 1, 2, and 3 can sum to 5.
✗ Incorrect
The code uses dynamic programming to count the number of ways to make amount 5 using coins 1, 2, and 3. The total ways are 7.
❓ Predict Output
intermediate2:00remaining
Output of Coin Change Ways with Different Coins
What is the output of the following C code that calculates the total number of ways to make change for amount 4 using coins {1, 3, 4}?
DSA C
#include <stdio.h> int countWays(int coins[], int n, int amount) { int dp[amount+1]; for (int i = 0; i <= amount; i++) dp[i] = 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, 3, 4}; int n = 3; int amount = 4; printf("%d\n", countWays(coins, n, amount)); return 0; }
Attempts:
2 left
💡 Hint
Count all combinations of coins 1, 3, and 4 that sum to 4.
✗ Incorrect
The code counts ways to make 4 using coins 1, 3, and 4. The total ways are 3.
🔧 Debug
advanced2:00remaining
Identify the Error in Coin Change Code
What error does the following C code produce when trying to calculate the number of ways to make change for amount 5 using coins {1, 2}?
DSA C
#include <stdio.h> int countWays(int coins[], int n, int amount) { int dp[amount]; for (int i = 0; i <= amount; i++) dp[i] = 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}; int n = 2; int amount = 5; printf("%d\n", countWays(coins, n, amount)); return 0; }
Attempts:
2 left
💡 Hint
Check the size of the dp array and the indices used.
✗ Incorrect
The dp array is declared with size amount (5), but code accesses dp[amount] (dp[5]) which is out of bounds causing segmentation fault.
🧠 Conceptual
advanced1:30remaining
Understanding DP Array Initialization in Coin Change
Why is dp[0] initialized to 1 in the dynamic programming solution for the coin change total number of ways problem?
Attempts:
2 left
💡 Hint
Think about how many ways there are to make zero amount.
✗ Incorrect
dp[0] = 1 means there is one way to make amount zero: by not choosing any coin.
🚀 Application
expert3:00remaining
Number of Ways to Make Change for Large Amount
Given coins {1, 5, 10, 25} and amount 100, what is the number of ways to make change using the standard dynamic programming approach?
Attempts:
2 left
💡 Hint
This is a classic coin change problem; the answer is a known value.
✗ Incorrect
The number of ways to make change for 100 cents using coins {1,5,10,25} is 242.