Design an expense sharing app 00:00:00 Make a prediction You will calculate each member's net balance in cents and preserve the zero-sum invariant after a payment or settlement. Ana pays thirty dollars for three friends including herself. Does the group now owe her thirty dollars? No. Her own ten-dollar share is already covered. The other two owe ten dollars each, so her net credit is twenty and the group balances sum to zero. 00:00:26 Camping friends and IOU twigs Three campers record food expenses on marked twigs. A twig carried from one camper's pile to another changes who owes whom but creates no new food or money. Count all piles together and the net change is zero. Record payments and shares independently, then derive balances. A settlement adjusts two members in opposite directions without rewriting the original expense. An expense sharing app remembers who paid for what in a group, works out what each person is owed, and proposes payments that make everybody even. No real money moves in it. It is a popular exercise, because everybody has shared a bill, and because the honest solution is short. What is left to judge is precision. 00:01:11 Balance three friends in cents Ana pays thirty dollars for three friends, including herself. Split into three shares of ten dollars each. Ana has paid twenty dollars beyond her own share. The other two friends each owe ten. Across the group the net balances sum to zero. Record a ten-dollar settlement from one friend to Ana, then check both affected balances. A repayment is a separate event, not a rewrite of who originally paid. Split nine dollars equally among three friends when one pays all nine. The payer's net credit is six dollars and the other two owe three each. Their net balances sum to zero. If the payer is also charged a full personal debt, that conservation check immediately fails. 00:01:57 Define the leftover cent rule Split ten dollars among three people using integer cents. The shares can be three dollars thirty-four, three thirty-three and three thirty-three. Choose a deterministic recipient for the extra cent so replaying the expense always produces the same ledger. Divide one thousand cents by three using integer arithmetic. Three equal floor shares total only nine hundred ninety-nine. Allocate the one extra cent using a documented ordering and repeat the request. A deterministic order should give the same person the remainder after a retry. Rounding each third independently to three dollars thirty-three leaves one cent unassigned. Specify a deterministic remainder rule and verify the shares sum to the expense. Simplifying who pays whom can reduce transfers, but keep original expense facts for audit and dispute handling. Splitwise distinguishes what a person paid from the share they owe. Subtracting those values gives the change in their net position. Its debt simplification feature can change which people settle with one another while preserving each person’s net result. This is why displaying a particular payment path and maintaining balances are separate jobs. 00:03:14 Try an interview follow-up Where do you store that Lee owes Asha 3? If the product only needs net balances, I do not store a separate debt from Lee to Asha. I subtract three from Lee's position and add three to Asha's. If the product must display pairwise obligations, I keep one signed record for each pair in a consistent order, so the two people's screens cannot drift. A fourth friend is added after the original three-way split was posted. Does changing the participants list automatically fix everyone's net balance? Reverse or replace the recorded allocation as one coordinated revision and post the new shares. Check that net balances still sum to zero and that a repeated update does not apply the correction twice. 00:04:02 What to take with you Keep money in whole cents. Allocate divides by weights: everybody rounded down, and the rest goes out by largest loss. The parts add up by construction. A split makes shares, an expense stores them, and balances book them as moves from one person to another. The sum stays zero, and that is the test.