CSE Roadmap

Core tier · about 4 hours · 6 parts

Part 5 of 6 · 3 min read

Edge cases and changes

Rounding, history and groups.

Step 5: edge cases, tests and change requests

Edge cases:

Case Behaviour
Amount zero or negative Refused
Split among zero people Refused (division by zero otherwise)
Payer not in the sharer list Allowed, since someone may pay for others only
Same person listed twice Decide: refuse, or count twice. Refusing is safer
Leftover cents Given to named people by a fixed rule, so results repeat exactly
Everyone balanced Simplify returns an empty list
Many threads Make expense and settle synchronized, since each updates several balances

On rounding, say which rule is used and why. Handing leftovers to the first people is easy to explain and is the same on every run. A random choice would be fair over time and impossible to test.

Tests to write first: each row of the run table; the sum of balances after every step, which must be zero; a split of 100.00 by 3 that adds up; each refused input; and a random run of 1,000 expenses that ends with a zero sum.

Change requests and the part that changes:

  • "Split by shares, such as 2 shares for adults and 1 for children." A new Split class.
  • "Several groups, each with its own books." A Group object that holds one ledger.
  • "Show who owes whom, not only the net." Keep a pairwise map as well, or keep the list of expenses and compute it on demand. Simplifying hides this detail.
  • "Edit or delete an expense." Keep an expense list, and rebuild the balances from the list.
  • "Several currencies." An amount type with a currency, and rates at the time of the expense.
  • "Notify people of new expenses." An observer on the ledger (Level 4, module 2).
  • "Recurring expenses such as rent." A scheduler that creates expenses.

Common mistake

Using double for money, or splitting 100.00 three ways as 33.33 each. The three shares add to 99.99 and one cent vanishes. Use whole cents and hand out the leftover.

Interview angle

Answer in your head first, then open it.

How do you store money?

Whole cents in a long, or a decimal type with a fixed scale. Never a binary floating-point number.

How do you split 100 by 3?

3334, 3333 and 3333. The leftover cent goes to a named person by a fixed rule.

How do you keep the books balanced?

Each expense credits the payer the total and debits the shares, which add to the total, so the sum stays zero.

How do you simplify debts?

Match the largest creditor to the largest debtor, repeat, and get at most n - 1 payments.

Check yourself: 1. Which change request needs only a new Split class? 2. Why does editing an expense need the list of past expenses? 3. Why is a random leftover rule hard to test?

Words you will meet

Word Meaning
Ledger The book that records who owes what
Payer The person who paid the bill
Share The part of an expense that one person must bear
Settle To pay a debt so that the balance moves toward zero
Creditor and debtor A person who is owed money, and a person who owes it