Build an Immutable Money Type With Currency Safe Rounding
Last updated: November 29, 2026
Quick Overview
Implement a Money value object that stores amounts as decimals rounded to the currency's minor units with banker's rounding, rejects mixed-currency arithmetic, and can split an amount into n parts that add up exactly.
Infosys
November 29, 20260
0
0 solved
Implement a Money value object that stores amounts as decimals rounded to the currency's minor units with banker's rounding, rejects mixed-currency arithmetic, and can split an amount into n parts that add up exactly.
Infosys engineers on banking and insurance accounts write a lot of code that touches money, and a phone screen often probes whether you know why floats are wrong for it. The interviewer is checking value semantics, immutability, decimal arithmetic, and the classic penny-allocation problem.
What the Interviewer Expects
- Explain why binary floating point is unsafe for currency
- Use a decimal type and quantize to the currency's minor units
- Make the object immutable with value equality
- Refuse arithmetic across currencies
- Allocate remainders so parts sum exactly to the original
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- Where should currency conversion live, and why not on Money itself?
- How would you persist this in a relational database without losing precision?
- Some taxes require rounding half up, not half even. How would you support per-rule rounding?
- How would you allocate by ratios such as 70/20/10 instead of equal parts?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
Amounts are exact decimals, rounded to 2 places for INR or USD, 0 for JPY, 3 for KWD. Rounding mode is half even to avoid systematic bias in large batches. Money is immutable and compares by value. Ad...
High-Level Design
A frozen dataclass holding a Decimal and a currency code. Normalize in __post_init__: convert through str so 0.1 does not drag in float error, then quantize. Arithmetic returns new objects. ...