Algorithm possible amounts overpaid for a specific price, based on denominations
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Given a target price and allowed denominations, a common task is to compute all possible overpayment amounts. This appears in cash systems, voucher handling, and optimization of payout strategies. The challenge is not only finding one valid payment, but characterizing the full space of reachable overpay values under clear limits.
Core Sections
1. Formalize the problem and constraints
Define the input first:
price: non-negative integer target.denominations: positive integer set, such as one, five, ten.- Optional limits, such as maximum number of items used or maximum total paid.
Define output clearly:
- Sorted unique list of
paid - pricevalues wherepaidis reachable and not smaller thanprice. - Include zero if exact payment is reachable.
Constraints are essential. Without limits, the set of overpay values can be infinite when denomination one exists. Most real systems cap by coin count, note count, or a maximum overpay threshold.
2. Dynamic programming for reachable totals
A practical approach is to compute reachable totals up to an upper bound, then derive overpay values. This is deterministic and easy to explain.
This gives all overpay values in the bounded range. The max_total bound should come from domain rules, not from guesswork.
3. Add coin-count limits with breadth-first search
If the system restricts how many pieces may be used, model states as total and count. Breadth-first search is a clean fit because each expansion adds one denomination.
This version prevents runaway search while preserving correctness under count limits.
4. Complexity and pruning
For bounded dynamic programming, complexity is roughly proportional to max_total multiplied by number_of_denominations. For count-limited search, complexity depends on branching factor and maximum depth.
Useful pruning ideas:
- Remove duplicate denominations.
- Remove denominations larger than the maximum reachable total if exact policy allows.
- Compute the greatest common divisor of denominations. If price parity conflicts with that divisor, exact payment may be impossible, and overpay starts above zero.
These small checks can reduce work significantly for large datasets.
5. Validation and operational usage
Validation should compare algorithm results with a brute-force reference on small inputs. This catches logic mistakes before scaling up.
Operationally, store metadata with each result set:
- input denominations
- bound assumptions
- whether exact payment exists
- minimum non-zero overpay
That metadata makes audits easier when payout behavior is questioned later.
Common Pitfalls
- Forgetting to apply a bound, which can make the search effectively unbounded.
- Mixing denomination count limits with total-value limits without documenting precedence.
- Returning duplicate overpay values instead of unique sorted results.
- Ignoring denomination normalization, causing unnecessary state explosion.
- Skipping reference checks and trusting complex pruning logic without verification.
Summary
- Overpay analysis must start from explicit bounds and output rules.
- Bounded dynamic programming is a strong default for reachable totals.
- Breadth-first search handles coin-count constraints cleanly.
- Lightweight pruning can cut runtime without harming correctness.
- Keep assumptions and metadata with results for auditability and reuse.

