Build a shared-expense tracker. Three people split a bill of ten pounds evenly - write the classes, and tell me where the missing penny goes.
A Splitwise-style machine coding answer turns on one invariant: the shares of an expense must sum to the expense total exactly, in every currency, forever. That forces money into integer minor units and forces a stated rule for allocating the remainder.
Answer
Short answer
In a Splitwise-style machine coding round, the core invariant is that every expense split must add back to the exact expense total. Store money as integer minor units or an exact decimal type, never double, and allocate any leftover penny by a deterministic rule. The strongest design keeps balances derived from expenses and validates the sum-to-total invariant in one place.
Keep machine coding explicit in the answer because that is the concept the interviewer is actually trying to test. A good machine coding explanation names the trade-off, the failure mode, and the evidence you would use before choosing.
Was this answer helpful?
Likely follow-ups
- The same three people split fifty bills. How do you stop one of them always paying the extra penny?
- Somebody edits an expense from last month. What happens to the balances, and to a settlement already made?
- Two participants are in different currencies. What do you store on the expense, and what do you refuse to compute?
- Your settlement suggests four transfers and a participant finds three. Is your algorithm wrong?
Related questions
- You may use an AI coding assistant in the machine-coding round. What do you still write yourself, and what do you ask it for?mediumAlso on machine-coding and invariants5 min
- Build an in-memory key-value store with per-key TTL, plus a snapshot operation that gives a consistent copy while writes keep coming in.hardAlso on machine-coding6 min
- Two candidates write the same class and one passes. What did the other one not say in the ten seconds before typing?mediumAlso on machine-coding5 min
- A bundle can contain products or other bundles, and the pricing code should not care which it is holding. How would you model that?mediumAlso on invariants6 min