Coin Change II
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that breaks a naive Coin Change II solution is counting 2+2+1 and 1+2+2 as different answers. Bloomberg reported this one in January 2021, and it's a clean unbounded knapsack counting problem. You get distinct denominations, an amount up to 5000, and you return the number of unordered combinations. The pattern is dynamic programming, and the whole thing hinges on one loop ordering choice. If your head goes blank when the OA timer starts, StealthCoder is the safety net that runs invisibly and hands you the solution. But you can learn this in ten minutes, so read on.
The problem
Given distinct positive denominations coins and a nonnegative amount, return the number of unordered combinations that sum exactly to amount. You may use each denomination any number of times. Different orders of the same multiset count once. Function countCoinChangeCombinations(coins: int[], amount: int) → long Examples Example 1 coins = [1,2,5] amount = 5 return = 4 The combinations are 5; 2+2+1; 2+1+1+1; and five 1s. Example 2 coins = [2] amount = 3 return = 0 No number of 2s sums to 3. Constraints 1 <= coins.length <= 100. 1 <= coins[i] <= 5000 and denominations are distinct. 0 <= amount <= 5000. The answer fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Use a 1D array dp of size amount+1 with dp[0] = 1. Loop over each coin in the outer loop, then loop s from coin to amount in the inner loop, doing dp[s] += dp[s - coin]. That ordering is the trick. Coins outside, amounts inside, means each combination gets built in one fixed coin order, so you count multisets and not permutations. Flip the loops and you count ordered sequences, which gives the wrong answer on example 1 (you'd get 9, not 4). Other pitfalls: amount = 0 must return 1 because the empty combination counts, and the result needs a 64-bit type since the statement says it fits a signed 64-bit integer. Time is O(n * amount), space is O(amount). If you blank during the live OA, StealthCoder is the hedge that reads the problem and gives you this loop structure in real time.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Coin Change II cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as coin change ii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Coin Change II FAQ
What's the trick in Coin Change II?+
Put the coin loop on the outside and the amount loop on the inside. That makes each combination counted once in a fixed coin order. Swapping the loops counts ordered sequences instead, which is a different problem and gives wrong answers like 9 on the [1,2,5] example.
What should the function return when amount is 0?+
Return 1. The empty combination sums to zero, and dp[0] = 1 is the base case that seeds every other state. Skipping this gives 0 for everything. It's the most common edge case in this problem, so test it before you submit.
How hard is this problem really?+
It's a medium. The recurrence is short, but candidates trip on loop order and on understanding why it matters. If you've seen unbounded knapsack before, it takes about ten minutes. If not, trace example 1 by hand once and the logic clicks.
Do I need a 2D DP table?+
No. A 2D table of coins by amount works and is easier to reason about, but you can collapse it to a 1D array because each row only depends on the current row and the previous one. The 1D version is shorter and uses O(amount) space.
How do I prepare for this in 48 hours?+
Write the 1D solution from memory twice. Then trace [1,2,5] with amount 5 and [2] with amount 3 by hand, and test amount = 0. Also try swapping the loops once to see the wrong output. That covers the pattern and the main failure mode before your Bloomberg style OA.