The Coin Change II interview question is a classic variation of the original Coin Change problem. Instead of finding the minimum number of coins, you need to find the total number of unique combinations that make up a target amount. You are given an array of coin denominations and an infinite supply of each. This problem emphasizes counting distinct sets rather than permutations.
Amazon, Google, and Bloomberg frequently use the Coin Change II coding problem to test a candidate's grasp of Dynamic Programming (DP) and specifically their ability to avoid double-counting. It’s a subtle but critical distinction between "how many ways to reach a sum" and "how many unique combinations exist." Interviewers want to see if you can manipulate the order of loops to achieve the correct counting logic.
The problem uses Bottom-Up Dynamic Programming. The key to counting unique combinations (and avoiding permutations) is the order of the nested loops. You must iterate through the coins first and then the amounts.
dp[i] stores the number of ways to make amount i.coin:
amount from coin to target:
dp[amount] += dp[amount - coin]Coins: [1, 2], Amount: 4
dp[0] = 1, all other dp values to 0.1:
dp[1] += dp[0] = 1dp[2] += dp[1] = 1dp[3] += dp[2] = 1dp[4] += dp[3] = 1dp array: [1, 1, 1, 1, 1]2:
dp[2] += dp[0] = 2 (Ways: {1,1}, {2})dp[3] += dp[1] = 2 (Ways: {1,1,1}, {1,2})dp[4] += dp[2] = 3 (Ways: {1,1,1,1}, {1,1,2}, {2,2})
Result: 3.{1, 2} and {2, 1} are counted as two different ways).dp[0] = 1 (there is exactly one way to make an amount of 0: using no coins).Always clarify if the interviewer wants "combinations" (order doesn't matter) or "permutations" (order matters). This distinction completely changes the loop structure of your DP solution.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Best Time to Buy and Sell Stock with Cooldown | Medium | Solve | |
| Partition Array for Maximum Sum | Medium | Solve | |
| Triangle | Medium | Solve | |
| Find the Maximum Length of Valid Subsequence I | Medium | Solve | |
| Maximum Absolute Sum of Any Subarray | Medium | Solve |