17Coin change with dollar amounts (dynamic programming)

mediumpython
45:00

17 Coin change with dollar amounts (dynamic programming)

An Airbnb MLE loop from April 2026 (reported that August) had a coin change variant with float denominations (1 report), and prep sites list two more exact-sum DP questions for Airbnb MLE rounds (2 sources).

Set a 45-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/17_coin_change_floats -k level1. Earlier levels must keep passing.

Amounts and coin values are dollars as floats, like 0.25, and each is a whole number of cents. Never do arithmetic on the floats: 0.1 + 0.2 != 0.3, and 1.15 * 100 is 114.99999999999999.

Level 1. min_coins(amount, coins): the fewest coins that add up to exactly amount, or -1 if no combination does, and 0 for an amount of zero. Each coin can be used any number of times. Be ready to say why greedy fails for some coin sets, and what your table costs in time and space.

Level 2. count_ways(amount, coins): how many different combinations add up to exactly amount. Order doesn't matter, so 5 + 1 and 1 + 5 are one combination. Zero has one combination: no coins.

Level 3. coins_used(amount, coins): the coins of one fewest-coins answer, as the values you were given (the same floats, not cents), in any order, as a list. [] for zero, and None if no combination works.

Level 4. The till holds a limited supply. min_coins_limited(amount, supply) takes a list of (coin, count) pairs and returns the fewest coins that add up to exactly amount, using each coin at most count times, or -1.

Follow-up: very large amounts.

solution.py
Run the tests to see which pass.