AlgoViz

Design an expense sharing app

medium 80 min

Divide bills between friends without losing a cent, keep one balance for each person that always adds up to zero, and settle with at most n - 1 payments.

Video lesson · Chapter 1 of 6

Make a prediction

0:00 / 4:24

See it run

Bills become shares, shares become moves, and the sum stays zero.

Operations

Add a bill

A tent for 10, shared by Asha, Lee and Noor.

Step 1 of 7

Asha has paid 10

ashacallertripGroupbillsnonelockfreetent splitEqualSplitpeopleA, L, NbalancesBalancesasha0lee0noor0sam0add(asha, 10, tent)

The group gets the payer, the sum and a way to divide.

Step 1: Asha has paid 10

def add(self, payer, cents, split, note):    if cents <= 0:        raise BadExpense("a bill is more than zero")    shares = tuple(split.shares(cents))    people = [s.person for s in shares]    if len(set(people)) != len(people) or not known(payer, *people):        raise BadExpense("unknown, or named twice")    if sum(s.cents for s in shares) != cents:        raise BadExpense("the shares do not add up")    with self._lock:        expense = Expense(next(self._ids), payer, cents, shares, note)        self._expenses[expense.id] = expense        self._post(expense, 1)        return expense
Cost
O(n log n)
Guarantee
Sum stays 0

Everything that can be refused is refused before the lock.

In the real world

The same expense sharing doing real jobs. Pick one and step through it.

Every row adds up to zero.

Step 1 of 5

The tent

AshaLeeNoorSamSum
tent+6-3-300
boat
now
pay 1
pay 2
end

Asha paid 10 and used 4.

Step 1: The tent

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Money as a float

    critical

    Ten times 0.10 is not 1.00, and balances never reach zero.

    Wrong
    share = total / len(people)
    Right
    parts = allocate(cents, [1] * len(people))
  • Rounding each share

    critical

    33.33 three times is 99.99. The cent has no owner.

    Wrong
    share = round(100 / 3, 2)
    Right
    parts = [c // n + (i < c % n) for i in range(n)]
  • Two copies of a debt

    critical

    They disagree as soon as one step is missed.

    Wrong
    lee.owes[asha] += 3asha.owes[lee] -= 3
    Right
    balances.move(lee, asha, 3)
  • Divide before multiply

    major

    999 // 10000 is 0, and everybody pays nothing.

    Wrong
    cents // whole * w
    Right
    cents * w // whole
  • Computing old shares again

    major

    After a change of the rule, removing a bill leaves cents behind.

    Wrong
    for s in expense.split.shares(expense.cents): undo(s)
    Right
    for s in expense.shares: undo(s)
  • A kind of expense for each split

    minor

    The record needs code to be read, and combinations multiply.

    Wrong
    class PercentExpense(Expense): ...
    Right
    group.add(payer, cents, PercentSplit({...}), note)

Interview tips

  • Open with

    • Whole cents.
    • The sum is zero.
    • A split makes shares.
  • Reject aloud

    • Debt is worked out.
    • User is a string here.
    • No expense subclasses.
  • Prove it

    • 10.00 by three.
    • Each row adds up.
    • All at zero at the end.
  • Never

    • round(total / n).
    • Both users' maps.
    • Percent as a double.