By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This problem appears in 1 out of every 3 onsite interviews—master it, and you prove you can handle dynamic programming, greedy algorithms, and edge cases under pressure."
Before tackling Coin Change, ensure you understand: 1. Dynamic Programming (DP) – Memoization, tabulation, and state transitions. 2. Greedy Algorithms – When they work (and when they don’t). 3. Breadth-First Search (BFS) – For finding the shortest path (minimum coins).
dp[i] = min(dp[i], dp[i - coin] + 1)
minCoins(amount, coins)
dp[0] = 0
dp[i] = min(dp[i - coin] + 1)
Repeat this checklist for every Coin Change problem:
Are there negative amounts? (No, but confirm.)
Choose the Right Approach
Greedy works? → Only for canonical systems (rare in interviews).
Define the DP State
dp[i]
i
dp[i][j] = minimum coins to make amount i using first j coins (if order matters).
dp[i][j]
j
Base Case
dp[i] = ∞ (or a large number) for unreachable amounts.
dp[i] = ∞
State Transition
For each coin, update dp[i]: python dp[i] = min(dp[i], dp[i - coin] + 1)
python dp[i] = min(dp[i], dp[i - coin] + 1)
Optimize Space (If Possible)
Use a 1D array instead of 2D if order doesn’t matter.
Handle Edge Cases
No solution → return -1 or None.
None
Test with Examples
coins = [1,2,5], amount = 11
amount = 0
coins = []
Problem: Given coins of different denominations and an amount, return the fewest number of coins needed. If impossible, return -1.
-1
Framework Application: 1. Clarify: Coins can be reused (unbounded knapsack). 2. Approach: DP (Unbounded Knapsack). 3. DP State: dp[i] = min coins to make amount i. 4. Base Case: dp[0] = 0, dp[i] = ∞ for others. 5. Transition: python for coin in coins: for i in range(coin, amount + 1): dp[i] = min(dp[i], dp[i - coin] + 1) 6. Edge Cases: amount = 0 → 0, no solution → -1.
dp[i] = min coins to make amount i
python for coin in coins: for i in range(coin, amount + 1): dp[i] = min(dp[i], dp[i - coin] + 1)
0
Full Code (Python):
def coinChange(coins, amount): dp = [float('inf')] (amount + 1) dp[0] = 0 for coin in coins: for i in range(coin, amount + 1): dp[i] = min(dp[i], dp[i - coin] + 1) return dp[amount] if dp[amount] != float('inf') else -1
Time Complexity: O(amount × coins) Space Complexity: O(amount)
Why This Works: - DP builds up solutions from smaller amounts. - Each dp[i] is the minimum of all possible coin choices.
Problem: Given coins and an amount, return the number of combinations that make up that amount. Coins can be reused.
Framework Application: 1. Clarify: Order doesn’t matter (e.g., [1,2] is same as [2,1]). 2. Approach: DP (count ways). 3. DP State: dp[i] = number of ways to make amount i. 4. Base Case: dp[0] = 1 (one way to make 0). 5. Transition: python for coin in coins: for i in range(coin, amount + 1): dp[i] += dp[i - coin]
[1,2]
[2,1]
dp[i] = number of ways to make amount i
dp[0] = 1
python for coin in coins: for i in range(coin, amount + 1): dp[i] += dp[i - coin]
def change(amount, coins): dp = [0] (amount + 1) dp[0] = 1 for coin in coins: for i in range(coin, amount + 1): dp[i] += dp[i - coin] return dp[amount]
Why This Works: - DP accumulates ways to form each amount. - Outer loop over coins ensures order doesn’t matter.
Problem: Same as Example 1, but optimize for minimum coins using BFS.
Framework Application: 1. Clarify: BFS finds the shortest path (minimum coins). 2. Approach: Treat amounts as nodes, coins as edges. 3. Queue: Start with 0, explore 0 + coin for each coin. 4. Visited Set: Avoid reprocessing the same amount.
0 + coin
from collections import deque def coinChange(coins, amount): if amount == 0: return 0 queue = deque([(0, 0)]) # (current_amount, num_coins) visited = set([0]) while queue: current, steps = queue.popleft() for coin in coins: next_amount = current + coin if next_amount == amount: return steps + 1 if next_amount < amount and next_amount not in visited: visited.add(next_amount) queue.append((next_amount, steps + 1)) return -1
Why This Works: - BFS explores all possible amounts level by level (shortest path first). - Visited set prevents redundant work.
dp
∞
visited
dp[i][j] = min(dp[i][j], dp[i - coin][j - 1] + 1)
"Alright, let’s lock this in. For Coin Change, here’s your battle plan:
Remember: Greedy only works for canonical coins—DP is your default. Test with small cases, and you’ll crush it. Good luck!
Now go ace that interview! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.