Design a Splitwise-style expense sharing system with balances and settle-up
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design an expense sharing system, similar to Splitwise, that tracks shared expenses among users and calculates balances.
Implement the following methods:
- `Splitwise()`: initialize an empty expense sharing system.
- `addUser(userId: string, name: string) → boolean`: register a new user. Returns `true` if successful and `false` if the user ID already exists.
- `addExpense(payerId: string, amount: number, participants: string[]) → string`: add an expense paid by one user and split equally among the participants.
- If the payer doesn't exist, return `"Payer not found"`.
- If any participant doesn't exist, return `"Participant not found"`.
- If the participants list is empty, return `"No participants"`.
- If the payer is not in the participants list, return `"Payer must be participant"`.
- Otherwise, split the expense equally and return `"Success"`.
- `getBalance(userId: string) → number | null`: get the net balance for a user.
- Returns `null` if the user doesn't exist.
- A positive balance means the user is owed money.
- A negative balance means the user owes money.
- `getBalanceBetween(user1: string, user2: string) → number | null`: get the balance between two users.
- Returns `null` if either user doesn't exist.
- Positive means user1 owes user2.
- Negative means user2 owes user1.
- `settleUp(payerId: string, payeeId: string, amount: number) → string`: record a payment from payer to payee.
- If the payer doesn't exist, return `"Payer not found"`.
- If the payee doesn't exist, return `"Payee not found"`.
- If the payer equals the payee, return `"Cannot settle with self"`.
- Otherwise, reduce the payer's debt to the payee and return `"Success"`.
Example call sequence:
```text
Splitwise()
addUser("u1", "Alice") -> true
addUser("u2", "Bob") -> true
addUser("u3", "Carol") -> true
addUser("u1", "Alex") -> false
addExpense("u1", 90, ["u1", "u2", "u3"]) -> "Success"
getBalance("u1") -> 60
getBalance("u2") -> -30
getBalanceBetween("u2", "u1") -> 30
addExpense("u2", 40, ["u1", "u3"]) -> "Payer must be participant"
addExpense("u4", 10, ["u4"]) -> "Payer not found"
settleUp("u2", "u1", 30) -> "Success"
getBalanceBetween("u2", "u1") -> 0
getBalance("u1") -> 30
addExpense("u3", 100, ["u1", "u2", "u3"]) -> "Success"
getBalance("u3") -> 36.67
getBalanceBetween("u1", "u3") -> 3.33
settleUp("u3", "u3", 5) -> "Cannot settle with self"
getBalance("u9") -> null
```
Alice pays 90 for three people, so Bob and Carol each owe her 30, and Bob's payment of 30 clears his part. Carol then pays 100 for all three, so Alice and Bob each owe her 33.333... Carol is owed 66.666... and still owes Alice 30, a net of 36.67 after rounding. Between Alice and Carol, Alice owes 33.333... and is owed 30, so Alice owes Carol 3.33 on net.
```hint What one number means
Pick a single quantity to store, such as how much one user owes another, and work out how each of the five methods reads or changes it.
```
```hint Payments and error order
A settle-up payment can be larger than the current debt, and an empty participant list passes some checks trivially. Trace both cases through your design before you write code.
```
### Constraints & Assumptions
- `1 <= userId.length, name.length <= 50`
- `0 < amount <= 100000`
- A participants list holds at most 100 IDs. An empty list can still arrive and must return `"No participants"`.
- At most 10,000 total calls will be made.
- All amounts are in cents (integers) or precise decimals.
- Balances are returned rounded to two decimal places.
- When several error cases apply to one call, return the first one in the order listed.
### Clarifying Questions to Ask
- When an expense does not divide evenly (100 among 3 people), should shares stay fractional, or should the leftover cents go to specific people?
- Can a settle-up payment exceed what the payer owes, and should the debt then flip direction?
- If a user ID appears twice in the participants list, is that an error, one share, or two shares?
- Does `getBalance` need to be constant time, or is a scan over all users acceptable at this call volume?
### What a Strong Answer Covers
- A representation of who owes whom, with a clear meaning for each stored number, and how every method reads or updates it.
- The validation cases in the stated order, with the exact return strings and the `null` results for unknown users.
- Correct sign conventions for both balance queries, including a payment that is larger than the debt.
- A plan for money precision: when values are rounded, and why rounded balances in a group can fail to sum to zero.
- Time and space complexity per method, and how to make `getBalance` constant time.
### Follow-up Questions
- How would you support unequal splits by exact amount, percentage or shares?
- How would you reduce a group's debts to as few payments as possible?
- How would you let users edit or delete an expense after it is recorded?
- What changes when many clients add expenses and payments at the same time?
Overview: Implement an expense sharing class that registers users, splits each expense equally among participants, reports net and pairwise balances, and records settle-up payments. It tests state modeling with nested hash maps, ordered input validation, sign conventions, and handling of uneven money splits.