Product design

Splitwise: keep shared expenses clear and reduce the final transfers

Splitwise helps friends, roommates and families record shared expenses and see how much each person still owes or should receive. Groups can keep the expense details while simplifying the final payment relationships.

Break down the problem and try your own design and implementation first. Then explore the reasoning to check states, edge cases, trade-offs, and ways to test your approach. The guide offers a reasoning path, not a complete implementation.

01
CODE EXERCISE

Split an amount without losing a cent

Implement a small equal-split module for an expense-sharing page. Splitting 10 yuan among three people cannot give everyone 3.33 yuan and lose one fen. The page and saved record should use the same calculation result. In this exercise, the API name cents refers to fen, the smallest unit of the yuan: 100 fen = 1 yuan.

  1. Input: Implement splitEvenly(totalCents, memberIds). totalCents is a nonnegative integer amount in fen, from 0 to 1000000. memberIds is a nonempty array of unique member IDs, with at most 20 members. Inputs are guaranteed to satisfy these conditions.
  2. Rules: Return a list of {id, cents} entries in input order. Split the amount as evenly as possible, assigning any remaining one-fen units to earlier members in the list. No two allocations may differ by more than one fen, and their sum must equal totalCents. The same input must produce the same result. Do not modify the input. The page handles formatting the amount as yuan for display.
  3. Acceptance examples: 1000 fen and [A,B,C] gives A:334, B:333, C:333; 2 fen and [A,B,C] gives A:1, B:1, C:0; 0 fen and [A,B] gives A:0, B:0.
  4. Deliverable: Implement the function in a language you know, run these three examples, check the sum of allocations, and briefly explain why the calculation interface uses integer minor units rather than display text. No accounts or real payments are required.
Explore the reasoning
  1. Separate money calculations from page formatting. Choose an internal unit that represents one fen exactly.
  2. Consider how to handle the portion that cannot be split evenly, keeping the difference between people within one fen and following the same order on repeated calculations.
  3. Check totals and ordering with 1000, 2 and 0 fen. In particular, when there are more people than available fen, check that you have not assigned one fen to everyone.