Splitwise LLD Quiz: Split Strategies and Settle Up Practice
Practise the Splitwise machine-coding problem: ten questions on splits, money in paise and debt simplification, and a Java ledger with a fourth split rule.
- Course: Low-Level Design study plan
- Module: Expense sharing
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: OpenJDK 18
Checkpoint: Expense sharing is the checkpoint that closes the Expense sharing module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the module: split rules as strategies, money in paise, balances that always add up to zero, and the greedy settle-up.
How it works. Ten questions and one program. You need 70% on the questions and the program accepted to clear the module. Retake it as often as you like; your best score counts.
Before you start, make sure you can answer these from memory:
- Why is a
doublethe wrong type for money, and what replaces it? - Where do leftover paise go in an equal split, and in a percent split?
- When does a split refuse its input, and what must not happen before it does?
- Which two members does each greedy settle step pick, and how much moves?
- What changes in the code when a new split rule is added?
The program adds a fourth rule, shares by weight, to the three you have written. If the Split interface is right, it is one more class; if it is not, you will feel it now.
Common questions
How do you add a new split type to an expense sharing app?
Write one more class that implements the Split interface, such as a SharesSplit that divides by weights like 2 : 1 : 1. Expense and BalanceSheet call the interface and never change: the open/closed principle at work.
Is the greedy debt simplification optimal?
Not always. It needs at most n - 1 payments, but the true minimum number of transfers can be lower when a group of debts cancels inside a subset of members; finding that minimum is NP-hard, so greedy is the practical answer.
How do you check an expense ledger is consistent?
Assert that the members' net balances add up to zero after every expense and every payment. Money only moves between members, so any other total means a split or rounding bug.
Exercise
A fourth split, and settle up
Extend the ledger with a fourth split rule and the settle-up.
Input. As in the lesson's first program: the members on the first line, then commands, money as paise.
EXPENSE payer amount TYPE …withTYPEone ofEQUAL,EXACT,PERCENT(exactly as before) or the newSHARES u1 w1 u2 w2 …: whole-number weights, each at least 1, elseshares must be positive. Each share isamount × w / total weight, floored to the paisa, with the leftover to the first member listed. The checks and the printed line are as before.BALANCES: as before.SETTLE: while anyone is owed money, the member who owes the most pays the member owed the most the smaller of the two amounts. Ties go to the earlier name. Printdebtor pays creditor 400.00for each payment and thentransfers: N. The payments are applied, so every balance is zero afterwards.
Example
ira jo ken
EXPENSE ira 1200 SHARES ira 2 jo 1 ken 1
SETTLE
prints
ira paid 1200.00: ira 600.00, jo 300.00, ken 300.00
jo pays ira 300.00
ken pays ira 300.00
transfers: 2