← Questions

Fewest coins when change can be given back

0
SnowflakeCodingLast asked Mar 2026

A coding interview question asked in Snowflake's two coding screens round, last asked Mar 2026. It tests dynamic programming, greedy and coins.

You must pay an exact amount using coins of the given denominations, but you are allowed to overpay: the other side then gives you change, also in the same denominations, so the net amount transferred equals amount. Return the fewest coins that change hands in total, counting both the coins you pay and the coins you receive. Each denomination is available in unlimited supply on both sides.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • min_coins_with_change(coins: list[int], amount: int) -> int returns the minimum total number of coins paid plus coins returned, or -1 if the amount cannot be settled at all.

Example

min_coins_with_change([1, 5, 10, 25], 99)  ->  5    # pay 4 x 25 = 100, get 1 back; plain coin change needs 9
min_coins_with_change([1, 7], 6)           ->  2    # pay 7, get 1 back
min_coins_with_change([5, 7], 1)           ->  5    # pay 3 x 5 = 15, get 2 x 7 = 14 back
min_coins_with_change([2, 4], 3)           -> -1    # every transfer is even
min_coins_with_change([1, 5, 10, 25], 30)  ->  2    # 25 + 5, no change needed

Constraints

  • 1 <= len(coins) <= 20, all denominations distinct positive integers.
  • 0 <= amount <= 10^4; an amount of 0 needs 0 coins.

Clarifying questions

  • Can both sides use every denomination, and is supply unlimited?
  • Is it allowed to pay and receive the same denomination (it never helps, but the model should say so)?
  • Do we only need the count, or also which coins are paid and returned?
  • How large can the amount and the largest coin be?

What a strong answer covers

  • The standard unbounded coin-change DP dp[v] = fewest coins summing to v, and the reduction: the answer is the minimum over overpaid totals x >= amount of dp[x] + dp[x - amount].
  • A finite, justified upper bound on how far past amount the overpaid total needs to go (stated in terms of the largest coin), with the argument for why overpaying further never saves coins.
  • The infeasible case: settling is possible only when amount is a multiple of the gcd of the coins.
  • Complexity: O((amount + bound) * len(coins)) time and O(amount + bound) space.
  • Why greedy fails, with a small counterexample, and tests for amount 0, a single coin, and unreachable amounts.

Follow-ups

  • Return the actual coins paid and returned, not only the count.
  • Each side has a limited number of coins of each denomination.
  • Minimize the coins you pay first, then the change, instead of the total.

Where it's asked

  • Snowflake · Two coding screens · Last asked Mar 202660 min · Two back-to-back live coding rounds, often on the same day; some loops pair coding with system design · Varies: some screens forbid AI, some 2026 screens allow it. The scheduling email says.

What it tests

dynamic programming · greedy · coins

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding