Bird
Raised Fist0
Interview Prepdp-knapsackmediumAmazonGoogle

Coin Change II (Count Ways)

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
</>
IDE
def change(amount: int, coins: list[int]) -> int:public int change(int amount, int[] coins)int change(int amount, vector<int>& coins)function change(amount, coins)
def change(amount, coins):
    # Write your solution here
    pass
class Solution {
    public int change(int amount, int[] coins) {
        // Write your solution here
        return 0;
    }
}
#include <vector>
using namespace std;

int change(int amount, vector<int>& coins) {
    // Write your solution here
    return 0;
}
function change(amount, coins) {
    // Write your solution here
}
0/10
Common Bugs to Avoid
Wrong: Counting permutations instead of combinations (too large count)Looping amounts outer and coins inner, causing order-sensitive counting.Change loop order: iterate coins outer loop, amounts inner loop to count combinations.
Wrong: Zero ways returned for amount=0Not initializing dp[0] = 1 to represent empty combination.Initialize dp[0] = 1 before DP iteration.
Wrong: Non-zero ways returned when coins array is emptyNot handling empty coins input properly.Return 0 for any positive amount if coins array is empty.
Wrong: Treating problem as 0/1 knapsack limiting coin usage to onceIncorrect DP update disallowing multiple usage of same coin.Allow unlimited usage by updating dp in coin outer loop and amounts inner loop.
Wrong: TLE on large inputsUsing brute force recursion or exponential approach.Implement bottom-up DP with O(n*amount) time complexity.
Test Cases
t1_01basic
Input{"amount":5,"coins":[1,2,5]}
Expected4

The four combinations are: 5=5; 5=2+2+1; 5=2+1+1+1; 5=1+1+1+1+1 (order does not matter).

t1_02basic
Input{"amount":3,"coins":[2]}
Expected0

No combination of coin 2 can make amount 3.

t2_01edge
Input{"amount":0,"coins":[1,2]}
Expected1

Only one way to make amount 0: use no coins (empty combination).

t2_02edge
Input{"amount":3,"coins":[]}
Expected0

No coins means no way to form any positive amount.

t2_03edge
Input{"amount":10,"coins":[10]}
Expected1

Only one combination: using the single coin once to make amount 10.

t2_04edge
Input{"amount":7,"coins":[2,4]}
Expected0

No combination of 2 and 4 can make 7.

t3_01corner
Input{"amount":5,"coins":[1,3,4]}
Expected3

Combinations: 1+1+1+1+1, 1+1+3, 1+4, 3+3 is invalid (exceeds amount). Valid are 4 combinations total.

t3_02corner
Input{"amount":4,"coins":[1,2]}
Expected3

Combinations: 1+1+1+1, 1+1+2, 2+2

t3_03corner
Input{"amount":7,"coins":[2,4]}
Expected0

No combination of 2 and 4 can make 7.

t4_01performance
Input{"amount":10000,"coins":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]}
⏱ Performance - must finish in 2000ms

Large input with n=100 coins and amount=10000; O(n*amount) DP must complete within 2s.

Practice

(1/5)
1. Consider the following code snippet implementing the minimum cost for tickets problem. What is the value of dp[0] after the loop completes for the input days = [1,4,6] and costs = [2,7,15]?
easy
A. 6
B. 7
C. 9
D. 4

Solution

  1. Step 1: Trace dp from the end

    dp[3] = 0 (base case). For i=2 (day=6): - 1-day ticket: cost=2 + dp[3]=2 - 7-day ticket: cost=7 + dp[3]=7 - 30-day ticket: cost=15 + dp[3]=15 Minimum = 2 -> dp[2]=2
  2. Step 2: Calculate dp[1] and dp[0]

    i=1 (day=4): - 1-day: cost=2 + dp[2]=4 - 7-day: cost=7 + dp[3]=7 - 30-day: cost=15 + dp[3]=15 Minimum=4 -> dp[1]=4 i=0 (day=1): - 1-day: cost=2 + dp[1]=6 - 7-day: cost=7 + dp[3]=7 - 30-day: cost=15 + dp[3]=15 Minimum=6 -> dp[0]=6
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    dp[0] correctly computed as 6 [OK]
Hint: Trace dp from end to start carefully [OK]
Common Mistakes:
  • Off-by-one in dp indexing
  • Miscomputing next_i with bisect
  • Confusing costs and dp sums
2. You are given a set of positive integers and need to partition it into two subsets such that the absolute difference of their sums is minimized. Which algorithmic approach guarantees an optimal solution for this problem?
easy
A. Greedy algorithm that sorts and assigns elements alternately to subsets
B. Dynamic programming using a subset-sum style approach to find achievable sums
C. Divide and conquer by recursively splitting the array into halves
D. Sorting and then pairing elements from opposite ends to balance sums

Solution

  1. Step 1: Understand problem constraints

    The problem requires minimizing the difference between sums of two subsets, which is a classic partition problem variant.
  2. Step 2: Identify algorithmic pattern

    Greedy or divide-and-conquer approaches do not guarantee minimal difference. Dynamic programming, specifically subset-sum style DP, can find all achievable sums up to total_sum, enabling minimal difference calculation.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    DP subset-sum approach guarantees optimal partition [OK]
Hint: Minimal difference requires subset-sum DP, not greedy [OK]
Common Mistakes:
  • Assuming greedy or sorting suffices for minimal difference
3. You need to find the minimum number of perfect square numbers that sum to a given integer n. Which algorithmic approach guarantees an optimal solution for this problem?
easy
A. Dynamic programming using unbounded knapsack pattern with bottom-up tabulation
B. Greedy approach picking the largest square first until the sum is reached
C. Simple recursion without memoization exploring all combinations
D. Sorting all squares and using binary search to find the closest sum

Solution

  1. Step 1: Understand problem constraints

    The problem requires the minimum count of perfect squares summing to n, which is a classic unbounded knapsack variant where items (squares) can be reused.
  2. Step 2: Identify algorithm that guarantees optimality

    Greedy fails because picking largest squares first can be suboptimal (e.g., n=12). Simple recursion is correct but inefficient. Binary search does not solve the combinatorial optimization. Bottom-up DP with unbounded knapsack pattern systematically explores all combinations and ensures optimality.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    DP approach always finds minimum count [OK]
Hint: Greedy fails; DP unbounded knapsack guarantees optimal [OK]
Common Mistakes:
  • Assuming greedy works for minimum perfect squares
  • Confusing top-down recursion with guaranteed efficiency
4. You are given an array of integers and a target integer. You want to assign either a plus or minus sign to each integer such that the resulting sum equals the target. Which algorithmic approach guarantees finding the total number of such assignments efficiently?
easy
A. Sorting the array and using two pointers to find pairs that sum to the target
B. Greedy algorithm that picks signs based on local sum minimization
C. Dynamic programming using a 0/1 knapsack-like approach with state representing achievable sums
D. Divide and conquer by splitting the array and combining results without memoization

Solution

  1. Step 1: Understand problem constraints

    The problem requires counting all sign assignments to reach a target sum, which involves exploring exponential combinations.
  2. Step 2: Identify suitable algorithmic pattern

    Greedy and two-pointer approaches fail because they do not consider all combinations. Divide and conquer without memoization is exponential. DP with states representing achievable sums efficiently counts all valid assignments.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    DP handles overlapping subproblems and sums efficiently [OK]
Hint: Counting sign assignments requires DP over sums [OK]
Common Mistakes:
  • Thinking greedy or sorting solves counting sign assignments
5. Consider the following buggy code for lastStoneWeightII. Which line contains the subtle bug that causes incorrect results on some inputs?
medium
A. Line 4: dp[0] = true is missing
B. Line 7: Outer loop over stones
C. Line 8: Inner loop iterates backwards
D. Line 10: Return statement calculation

Solution

  1. Step 1: Identify dp initialization

    dp[0] must be true to represent sum zero achievable with no stones; missing this means no sums are marked achievable.
  2. Step 2: Consequence of missing dp[0] = true

    Without dp[0] = true, dp array remains false, so no sums can be formed, causing the function to fail to find any valid partition.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Initializing dp[0] is critical base case for subset sum DP [OK]
Hint: dp[0] = true is base case for subset sums [OK]
Common Mistakes:
  • Forgetting dp[0] initialization
  • Iterating forward in inner loop causing double counting
  • Miscomputing return value