machine coding

Expense Splitter

Flatmates share bills: balances in paise, equal and exact splits, and a short settle-up plan.

Medium90 minutesJava · class ExpenseSplitter

the brief

What you are building

Rahul, Priya and Amit share a flat in Bengaluru. Rent, Wi-Fi, electricity and groceries are paid by whoever is at the shop or online first.

At the end of the month, nobody remembers who owes whom. They want an app like Splitwise: record every bill, show each person's balance, and suggest a short list of payments that settles everything.

You will build its core. Money is the one thing that must be exactly right.

must have

Requirements

  1. All amounts are paise (int; ₹1 = 100 paise). Never double.
  2. addEqualExpense(paidBy, amount, people): split equally among people. Each share is amount / n; the first amount % n people in the list pay 1 paisa more, so the shares add up to the bill exactly.
  3. The payer may or may not be in people (Rahul can pay a bill only for Priya and Amit).
  4. addExactExpense(paidBy, people, shares): people[i] owes shares[i]; the bill is the sum of the shares.
  5. balance(person): what others owe this person (positive) or what this person owes (negative). 0 for someone never seen.
  6. settleUp() returns a payment plan and changes nothing: repeat — the person who owes the most pays the person owed the most, min of the two amounts. Ties go to the name that comes first alphabetically.
  7. Each payment is written as "<debtor> pays <creditor> <paise>", e.g. "amit pays rahul 40000".

the contract

The Java API: class ExpenseSplitter

public ExpenseSplitter()
No people, no bills.
public void addEqualExpense(String paidBy, int amount, String[] people)
An equal split.
public void addExactExpense(String paidBy, String[] people, int[] shares)
An exact split.
public long balance(String person)
Net paise: + is owed, − owes.
public List<String> settleUp()
The greedy plan; at most n − 1 payments.
  • 1 ≤ amount, each share ≤ 10⁷ paise (₹1 lakh)
  • 1 ≤ people in one bill ≤ 20, no name twice in one bill
  • up to 2,000 bills and 100 people
  • balances can pass Integer.MAX_VALUE: use long

example session

Calls in, answers out

Three flatmates. ₹900 for Wi-Fi + electricity, then ₹300 of groceries, then a ₹1 bill that does not split evenly.

callreturnswhy
new ExpenseSplitter()—
addEqualExpense("rahul", 90000, new String[] {"rahul", "priya", "amit"})—30000 each; rahul is owed 60000
addExactExpense("priya", new String[] {"rahul", "amit"}, new int[] {20000, 10000})—priya paid 30000 for the other two
balance("rahul")40000+90000 − 30000 − 20000
balance("priya")0−30000 + 30000
settleUp()["amit pays rahul 40000"]
addEqualExpense("amit", 100, new String[] {"rahul", "priya", "amit"})—100 / 3 = 33 rest 1: rahul 34, priya 33, amit 33
balance("amit")-39933−40000 + 100 − 33
settleUp()["amit pays rahul 39933", "priya pays rahul 33"]the biggest debt is paid first

time plan

Your 90 minutes

  1. 10 minclarify — Paise or rupees? Who gets the leftover paisa? Can the payer be outside the split? Does settleUp change the balances?
  2. 10 mindesign — One balance per person is enough — you do not need who-owes-whom pairs. Splits are strategies: equal, exact, later percent.
  3. 35 mincore code — Both add methods and balance. Run the first half of the example. Then settleUp with two heaps.
  4. 25 minedge cases — Payer pays only for themself, payer outside the split, leftover paise, debts that cancel to 0, totals past the int range.
  5. 10 minpolish — Be ready to say why the greedy plan is short but not always the shortest possible.

Practise under real pressure: a 90-minute clock with the phases above. It keeps running if you reload the page.

your turn

Build it here

Write the whole class in the editor. Run replays the example session; Submit adds the hidden tests (edge cases and large random sessions). Stuck for 20 minutes? Open one hint at a time.

Build ExpenseSplitter in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    The reference, the data-structure reasoning and the rubric are hidden so you can build it first. Even a half-working attempt teaches you more than reading a perfect answer.

    Opening all three hints also shows it.

    revise

    Chapters that teach these structures