What is the coin change problem?

Why Interviewers Ask This

Mid-level Data Structures & Algorithms roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.

Answer

The Coin Change problem has two variants: (1) Minimum coins: given coin denominations and a target amount, find the minimum number of coins to make the amount. DP: dp[i] = minimum coins for amount i. Recurrence: dp[i] = min(dp[i - coin] + 1) for each coin ≤ i. Base: dp[0] = 0. Time: O(amount × coins). Space: O(amount). Example: coins=[1,2,5], amount=11 → dp[11] = 3 (5+5+1); (2) Number of ways: count distinct combinations to make the target. DP: dp[i] = number of ways to make amount i. Recurrence: dp[i] += dp[i - coin] for each coin ≤ i. Base: dp[0] = 1. This is the unbounded knapsack variant. Greedy approach: works ONLY for canonical coin systems (like US coins: 25, 10, 5, 1) but fails for arbitrary denominations (e.g., coins=[1,3,4], amount=6: greedy picks 4+1+1=3 coins, DP finds 3+3=2 coins). Related: Coin Change with limited quantity (bounded knapsack). Applications: (1) Currency systems; (2) Change-making machines; (3) Dynamic programming pedagogy; (4) Combinatorics problems. The coin change DP pattern (unbounded knapsack) applies to: word break, integer break, and other "ways to make X from components" problems.

Pro Tip

Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Data Structures & Algorithms answers easy to follow.