Quick revision
The short answer
The model is User, Group and an immutable Expense (payer, BigDecimal amount, list of Splits), with a SplitStrategy interface whose Equal, Exact and Percent implementations produce the splits and check that they add up to the amount. Balances are derived as a Map<User, BigDecimal> of net amounts per group, positive meaning the user is owed money. To settle up, we repeatedly match the largest creditor with the largest debtor using two priority queues, which needs at most n - 1 payments; finding the true minimum is NP-hard in general.
What to remember
In this answer, we built an expense-sharing model with immutable expenses, a SplitStrategy interface for equal, exact and percent splits, and BigDecimal money with deterministic distribution of leftover cents. Balances are derived per group from the expenses and always sum to zero. Settling up matches the largest creditor with the largest debtor using two priority queues, giving at most n - 1 payments in O(n log n), while the true minimum is NP-hard and only worth searching for in small groups.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The model is User, Group and an immutable Expense (payer, BigDecimal amount, list of Splits), with a SplitStrategy interface whose Equal, Exact and Percent implementations produce the splits and check that they add up to the amount. Balances are derived as a Map<User, BigDecimal> of net amounts per group, positive meaning the user is owed money. To settle up, we repeatedly match the largest creditor with the largest debtor using two priority queues, which needs at most n - 1 payments; finding the true minimum is NP-hard in general.
Why this question is asked
Splitting bills looks like a toy problem, but it has the three things interviewers want to probe in a mid-level design: a clean place for rules that vary (how an expense is split), money arithmetic that must add up to the cent, and a real algorithm at the end. It is also close to work many of us do, such as allocating costs or reconciling ledgers. In this answer, we will build the model, handle rounding so splits always add up, derive balances, and then design and trace the settle-up algorithm, including why it is good but not always optimal.
1The Model: Users, Groups, Expenses and Splits
A User is a person with an id. A Group is a set of users who share expenses, such as a trip. An Expense records who paid, how much, and how the amount is divided: a list of Splits, each saying how much one user owes for this expense. Money is a BigDecimal with two decimal places, never a double, because a double cannot represent 0.10 exactly and errors build up over hundreds of expenses.
The way an expense is divided varies: equally, by exact amounts, or by percentages. That variation goes behind an interface, SplitStrategy, so adding a new rule such as by shares (2 shares for adults, 1 for children) means adding one class. Each strategy turns an amount and a list of participants into splits, and validates its own input: exact amounts must sum to the total, percentages must sum to 100.
Notice that Expense is a record and checks its own invariant, the rule that must always hold, in the compact constructor: an expense whose splits do not add up can never exist. Whatever strategy produced the splits, the expense itself guarantees the total.
2Equal Splits and the Extra Cent
Dividing 100.00 among 3 people gives 33.333..., and we can only charge whole cents. If we round each share to 33.33, the shares add up to 99.99 and one cent disappears. If we round up, they add to 100.02. Either way the books do not balance.
The fix is to work in cents and hand out the remainder deterministically. 10,000 cents divided by 3 is 3,333 with a remainder of 1, so one person pays 3,334. Deterministically means the same input always gives the same result: we sort participants by id and give the extra cents to the first ones, so a re-run or an audit produces identical splits.
PercentSplit uses the same idea: compute each share, round down to cents, and distribute the leftover cents in a fixed order. ExactSplit only validates that the given amounts sum to the total.
3Deriving Balances
A balance is a user's net position in a group: everything they paid for others minus everything others paid for them. For each expense, the payer's balance goes up by the full amount and each split user's balance goes down by their share. The payer usually has a split too, which cancels part of their credit. Positive means the group owes this user money; negative means the user owes the group.
The important design choice is that balances are derived, not stored as the source of truth. Expenses are immutable records, and balances are computed from them, or kept in a cache that we can always rebuild. If someone edits an expense, we record a correction or a replacement, and the balances follow. That makes audits easy: any balance can be explained by listing the expenses behind it. If expenses can be deleted, an event log of added, corrected and deleted expenses keeps that history.
The balances always sum to zero, because every expense adds its amount once and subtracts the same amount across its splits. That gives us a cheap check to run in tests.
4Settling Up With Two Priority Queues
Settling up means producing a list of payments that brings every balance to zero. The naive way, each debtor paying each creditor what they owe from each expense, produces many small payments. We want fewer.
The greedy algorithm keeps creditors in one max-priority queue by amount owed to them, and debtors in another by amount they owe. Each round, the largest debtor pays the largest creditor the smaller of the two amounts. At least one of them reaches zero and leaves its queue; the other goes back with the remainder. Since each payment removes at least one person, and the last payment removes two, n people need at most n - 1 payments.
- Take the largest creditor C and the largest debtor Dtwo max-priority queues
- D pays C min(owed to C, owed by D)
- Both reach zero?Yesboth leave their queuesNothe one with a remainder goes back in its queue
- Queues empty?Yesdone: at most n - 1 paymentsNonext round
4 steps in order. Play them, or step through them one at a time.
Each round costs O(log n) for the queue operations, so the whole settle-up is O(n log n). For a group of friends it is instant.
5Why Greedy Is Good but Not Always Optimal
The greedy answer is not always the minimum. Take balances +6, +5, -5, -3 and -3. The optimal plan notices two independent groups that each sum to zero: the -5 pays the +5 directly, and the two -3 debtors pay the +6. That is 3 payments. Greedy pairs the largest amounts first: the -5 pays 5 to the +6, then the remaining amounts need three more payments, 4 in total.
Finding the true minimum means finding the largest number of subgroups that each sum to zero, because a subgroup of k people can always settle in k - 1 payments. That is a partition problem, and in general it is NP-hard: no known algorithm solves it efficiently for large groups. For small groups, say under 15 people, a search over subsets is affordable and could be offered as an option. For everyone else, the n - 1 bound of the greedy algorithm is good enough and easy to explain, which matters for a product people use with friends.
6Follow-up questions
How would you support multiple currencies?
Each expense keeps its original Money, an amount and a currency, and is never converted in place. Balances are kept per currency, so a user can be owed 40 EUR and owe 10 USD at the same time. When the group settles, it either settles each currency separately or converts to one settlement currency using a rate recorded with the date and source, so the conversion can be explained later. Rounding rules then follow the currency's minor units, for example none for JPY.
Why not store balances directly and update them on each expense?
We can cache them that way for speed, but they should not be the source of truth. If an expense is edited or a bug corrupts a balance, we need to rebuild balances from the immutable expenses. Storing only derived values loses the history that support and audits need.
How do you test the money logic?
With properties that must hold for any input: splits always sum to the expense amount, balances in a group always sum to zero, and after applying the settle-up payments every balance is zero. A property-based test that generates random expenses checks these far better than a few hand-written examples.
7Conclusion
In this answer, we built an expense-sharing model with immutable expenses, a SplitStrategy interface for equal, exact and percent splits, and BigDecimal money with deterministic distribution of leftover cents. Balances are derived per group from the expenses and always sum to zero. Settling up matches the largest creditor with the largest debtor using two priority queues, giving at most n - 1 payments in O(n log n), while the true minimum is NP-hard and only worth searching for in small groups.
Open with
I would model User, Group and Expense, where an Expense is an immutable record of payer, a BigDecimal amount and a list of Splits, and its constructor checks that the splits add up to the amount.
Then, if they want more
How an amount is divided goes behind a SplitStrategy interface with EqualSplit, ExactSplit and PercentSplit, each validating its input.
For equal splits I work in cents: 100.00 among three is 3,333 cents each with one left over, and I give the extra cent to the first user by a fixed order, so the parts always add up and a re-run gives the same result.
Balances are derived, not stored as truth: per group, a map from user to net BigDecimal, where the payer gains the amount and each split user loses their share, positive meaning they are owed money. Balances always sum to zero, which is a nice test.
For settle up, I put creditors and debtors into two max priority queues and repeatedly have the largest debtor pay the largest creditor the smaller amount, pushing back any remainder. Each payment removes at least one person, so it needs at most n minus one payments in n log n time.
It is not always the minimum; the true minimum means finding zero-sum subgroups, which is NP-hard, so I would only search for it in small groups.
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 official documentation for the version you use. 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 "Design an expense-sharing module in Java (like splitting bills among friends): users...": https://scrivio-interview-library.netlify.app/java/coding-problems/28d4da8ae3/