Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

You are writing guidelines for a payments platform used by twenty teams. How should they handle arithmetic overflow and numeric types for amounts, counts and identifiers?

I would choose a representation per kind of number: money as long minor units or BigDecimal with an explicit scale, never float or double; counts as int or long with Math.*Exact or range checks wherever outside input feeds arithmetic; and identifiers as long or opaque strings that nobody does arithmetic on. Amounts would live in a shared Money value type so the rules sit in one place, and the guidelines would be enforced by a shared library, static analysis rules and tests at boundary values rather than by documentation alone.

Why this question is asked

On a payments platform, numeric mistakes are not just bugs; they are money moving to the wrong place, failed reconciliation and possibly regulatory trouble. With twenty teams, the risk multiplies, because each team makes its own small decisions and the boundaries between their services are where values get converted. Interviewers ask this design question to see whether we can turn knowledge about overflow and precision into rules that scale across an organisation. In this answer, we will separate the three kinds of numbers, write the rule for each, design a shared Money type, look at how to enforce the rules, and finish with migrating an existing int amount field.

1Three Kinds of Numbers, Three Sets of Rules

The first step in the guideline is to stop treating all numbers alike. Amounts are decimal quantities of a currency, where every cent must be exact and rounding must follow business rules. Counts, such as the number of items, retries or transactions in a batch, are whole numbers where the danger is overflow. Identifiers, such as account or payment ids, only look like numbers; they are labels, and their only operations are equality and lookup.

Once teams see that a price, a quantity and an account id are different kinds of value, most of the rules follow naturally, and code reviews have clear language for what is wrong.

Numeric guidelines by domain
KindRepresentationArithmetic ruleNever
Amountslong minor units, or BigDecimal with fixed scaleMoney methods with exact checksfloat or double
Rates, FX, taxBigDecimal with stated precisionround once at defined pointsrounding per step by habit
Countsint or longMath.*Exact or range checks on inputunchecked int products
Identifierslong or opaque Stringequality onlyarithmetic or ordering logic
Over the wirestring decimal or integer minor unitscurrency always sent alongsideJSON floating numbers

2Amounts: Minor Units or BigDecimal, Never Floating Point

float and double are binary formats, so most decimal fractions, including 0.10, have no exact representation, and repeated arithmetic drifts by fractions of a cent. The guideline therefore bans them for money outright. Teams choose between two exact representations. A long count of minor units, such as cents, is fast, compact and simple for storing and adding amounts; with long, we can represent about 92 quadrillion dollars in cents, which is plenty. BigDecimal with an explicit scale is needed when we divide, apply percentages, convert currencies or handle currencies whose minor unit is not two digits.

Every amount also carries its currency. Adding 100 EUR to 100 USD must be an error, not 200 of something.

3A Shared Money Type

Rules in a wiki are forgotten; rules in a type are applied every time. A small shared library provides a Money value type, so that services never pass raw longs around as amounts.

Java
public record Money(long minor, Currency currency) {
public Money {
Objects.requireNonNull(currency);
}
public Money plus(Money o) {
requireSameCurrency(o);
return new Money(Math.addExact(minor, o.minor), currency); // overflow throws
}
public Money times(long quantity) {
return new Money(Math.multiplyExact(minor, quantity), currency);
}
public BigDecimal toDecimal() {
return BigDecimal.valueOf(minor, currency.getDefaultFractionDigits()); // 1999 EUR -> 19.99
}
private void requireSameCurrency(Money o) {
if (!currency.equals(o.currency)) throw new IllegalArgumentException("currency mismatch");
}
}
Money price = new Money(1999, Currency.getInstance("EUR"));
Money total = price.times(3); // Money[minor=5997, currency=EUR]

Notice that the type uses the exact methods internally, so no team can forget them, and that it fixes the scale from the currency's default fraction digits. The library also owns serialisation: in JSON, an amount is either a string such as "19.99" or an integer of minor units, always with a currency code, because many JSON parsers read numbers as double. Rounding modes for tax and currency conversion are defined once, with the finance domain owners, and exposed as named methods rather than left to each call site.

4Counts and Identifiers

For counts, the rule is about trust. Arithmetic on values that come from outside, such as quantities in a request, sizes in an uploaded file or page sizes from a client, goes through Math.addExact, multiplyExact and toIntExact, or through an explicit range check that rejects unreasonable input with a clear error. Counts that cannot exceed int in any realistic case may use int; anything that accumulates over time, such as totals, offsets or durations in milliseconds, uses long.

For identifiers, the rule is that they are opaque. They are long or String, they are compared with equals, and nobody computes the next id by adding one or infers order from them. Treating ids as numbers invites overflow when an id space grows past int, and invites bugs when ids from different systems are mixed. A wrapper type such as a PaymentId record makes it impossible to pass an account id where a payment id is expected.

5Enforcing It Across Twenty Teams

The guideline works only if it is enforced automatically. The shared library is the default way to handle money. Static analysis rules in the shared build configuration flag float and double fields or parameters with monetary names such as amount, price, fee or balance, and flag arithmetic on fields typed as identifiers; ArchUnit tests or custom Error Prone or PMD rules can express these. API schemas are linted so that monetary fields cannot be declared as floating numbers. Finally, the library and each service include tests at boundary values: amounts near Long.MAX_VALUE, quantities that overflow int, zero, negative refunds and currencies with zero or three fraction digits.

6Follow-up questions

How would you migrate an existing int-based amount field without downtime?

With an expand and contract migration. First, add a new long column (or a NUMERIC column) next to the old one and deploy code that writes both. Then backfill the new column from the old one in batches and verify that they match. Next, switch reads to the new column, behind a flag so we can roll back, and update API contracts to expose the new field while keeping the old one for existing consumers. When every reader uses the new field and monitoring shows no mismatches, stop writing the old column and finally drop it. Each step is backward compatible, so no deployment requires downtime.

Should we choose long minor units or BigDecimal as the platform default?

For storing and adding amounts, long minor units are simpler and faster, so I would make them the default inside a Money type. Calculations that divide or apply rates, such as tax, interest or currency conversion, use BigDecimal internally and convert back to minor units with a defined rounding mode. The type hides that choice from most callers.

What about currencies with three decimal places?

That is why the scale comes from the currency, not from a hard-coded 2. Currency.getDefaultFractionDigits returns 0 for JPY and 3 for currencies such as KWD, and the Money type uses it for formatting and conversion, so the same code handles them correctly.

7Conclusion

In this answer, we wrote guidelines that treat amounts, counts and identifiers differently: money as long minor units or BigDecimal with an explicit scale and never floating point, counts guarded by Math.*Exact or range checks wherever outside input enters, and identifiers as opaque long or String values without arithmetic. We put the amount rules into a shared Money type, enforced the rest with static analysis, schema linting and boundary tests, and migrated an int amount field with an expand and contract approach.

What to say in the interviewAbout 100 seconds aloud

Open with

I would start by splitting numbers into three kinds, because they need different rules. Amounts are money: they are stored as long minor units, like cents, or as BigDecimal with an explicit scale when we divide or apply rates, and never as float or double, because binary floating point cannot represent decimal fractions exactly.

Then, if they want more

Every amount carries its currency. Counts are whole numbers where the risk is overflow, so wherever user or external input feeds arithmetic, we use Math.addExact, multiplyExact, toIntExact or explicit range checks, and anything that accumulates uses long. Identifiers are labels: long or opaque strings, compared with equals, and never used in arithmetic.

To make this real across twenty teams, I would not rely on a document. I would provide a shared Money value type, a record with exact arithmetic inside, currency checks and the scale taken from the currency, plus its JSON serialisation as a string or integer minor units.

Rounding modes for tax and conversion are decided once with finance and exposed as named methods.

Then I would enforce it: static analysis rules in the shared build that flag float or double on monetary names and arithmetic on ids, schema linting for APIs, and tests at boundary values such as overflow, zero, refunds and currencies with zero or three decimals.

For existing int amounts, I would migrate with expand and contract: add the new field, dual write, backfill, switch reads, then drop the old one.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "You are writing guidelines for a payments platform used by twenty teams. How should they...": https://scrivio-interview-library.netlify.app/java/types-and-values/5e928a7ea4/