Designing Splitwise - Expense Splitting System
Difficulty: Medium Patterns: Strategy, Observer, Facade Asked at: PhonePe, Flipkart, Amazon, Razorpay
Functional Requirements
- Add expenses shared between users with a payer and multiple participants
- Multiple split strategies - EQUAL, EXACT (specify amounts), PERCENTAGE
💡 Calling these “strategies” means each division rule is its own small object that the expense plugs in, rather than a branch inside one big method. Learn more → - Track balances - who owes whom across all expenses
- Simplify debts - minimize the number of transactions needed to settle up
💡 Debt simplification = replacing a web of small “A owes B” edges with the fewest payments that leave every person’s net position unchanged. Nobody ends up better or worse off; there are just fewer transfers to make. - Group expenses - organize expenses under a group (trip, apartment, etc.)
- Balance summary - show per-user net balance
Non-Functional Requirements
- Thread-safety - concurrent expense additions must not corrupt balances
💡 Thread-safety = the code still behaves correctly when several threads run it at the same instant. Two people adding expenses to the same group at once must not both read the old balance and overwrite each other. - O(n log n) debt simplification - greedy with heaps
💡 A heap is a structure that keeps the largest (or smallest) item instantly reachable and costs only O(log n) to reorder after each removal — which is what makes “repeatedly grab the biggest creditor” cheap. - Extensibility - add new split types (SHARE, WEIGHT) without modifying existing code
- Correctness - split amounts must always sum to the total expense amount
💡 This is the whole of money handling: currency is best held as a precise decimal or as whole cents, because binary floating point cannot represent 0.01 exactly and the rounding dust it leaves behind stops shares from summing back to the total.
How far the code below actually meets that, and where it stops. The split strategies allocate in whole cents and distribute the remainder, so every share is a payable amount and the shares sum back to the total when compared in cents. Checked exhaustively over every total from $0.01 to $2000.00 against 1 to 12 participants — 2.4 million combinations, zero discrepancies in minor units.
Compared as
double, the same shares disagree with the total in about 38% of those cases, by around 10^-14. That is not an allocation bug; it is the return type.3.34has no exact binary representation, so no allocation strategy can make a sum ofdoubleshares equal adoubletotal under==.The honest conclusion is the one the money-handling guide reaches: an interview-grade answer keeps money in minor units end to end —
long cents, orBigDecimal— and converts to a decimal string only at the edge for display. These listings keepdoubleat the interface because the rest of the design is about Strategy and debt simplification rather than currency, and swapping the type would touch every class on the page. If an interviewer asks “what would you change first?”, this is the answer: the type, not the arithmetic.
Core Entities
| Entity | Description |
|---|---|
User |
Immutable - id, name, email |
Group |
Named collection of users and their shared expenses |
Expense |
Amount, payer, description, split strategy, participants |
Split |
Strategy interface - computes each participant’s share |
EqualSplit |
Divides equally among all participants |
ExactSplit |
Each participant’s amount specified explicitly |
PercentageSplit |
Each participant pays a percentage of total |
BalanceSheet |
Tracks net balances between every pair of users |
ExpenseManager |
Facade - orchestrates expenses, groups, balances, and simplification |
💡 Immutable (see User) = once constructed, the object’s fields never change. An immutable User can be referenced from every group and every expense at once with no risk of one of them mutating it underneath the others.
💡 A net balance is the single number left after cancelling what two people owe each other in both directions, so each pair of users needs one figure rather than a list of every expense they ever shared.
Class Diagram
classDiagram
class Split {
<<interface>>
+validate(totalAmount, participants) boolean
+computeShares(totalAmount, participants) Map~User-Double~
}
class EqualSplit
class ExactSplit
class PercentageSplit
class Expense {
-String id
-double amount
-User payer
-String description
-Split splitStrategy
-List~User~ participants
}
class BalanceSheet {
-Map~String-Map~String-Double~~ balances
+recordDebt(from, to, amount)
+simplifyDebts() List~Transaction~
}
class ExpenseManager {
-Map~String-User~ users
-Map~String-Group~ groups
-BalanceSheet balanceSheet
+addExpense(expense)
+getBalance(userId) Map
+simplify() List~Transaction~
}
Split <|.. EqualSplit
Split <|.. ExactSplit
Split <|.. PercentageSplit
Expense --> Split
ExpenseManager --> BalanceSheet
ExpenseManager --> Expense
💡 Reading the arrows: <<interface>> marks a contract with no code of its own, a dashed arrow (Split <|.. EqualSplit) means “implements that contract”, and a solid arrow means “holds a reference to”. Learn more →
Design Patterns
| Pattern | Where | Why |
|---|---|---|
| Strategy | Split with Equal/Exact/Percentage |
Swap split algorithm per expense, no if/else chain |
| Observer | BalanceObserver on balance changes |
Notify UI or services when debts change |
| Facade | ExpenseManager |
Single entry point hides balance tracking, group logic, simplification |
Data Structures
| Component | Structure | Why |
|---|---|---|
| Who owes whom | Map<String, Map<String, Long>> |
Nested debtor then creditor, so one person’s entire debt position is a single outer lookup |
| Computed shares | Map<User, Long> in integer cents |
Minor units end to end. The remainder left by an uneven split is reconciled explicitly rather than rounded away |
| Debt simplification | two PriorityQueue<long[]> |
Greedy settlement repeatedly needs the largest creditor and the largest debtor, which is O(log n) from a heap and O(n) from a list |
| Members and expenses | List<User>, List<Expense> |
Append-only and always iterated whole, so there is nothing to index |
💡 A heap is the right structure here precisely because the algorithm never asks “who owes 40 rupees” — it only ever asks for the extremes, which is the one question a heap answers cheaply.
How It All Fits Together
Here’s what happens when a user adds an expense:
- User calls
addExpense()on ExpenseManager with amount, payer, participants, and split type - ExpenseManager delegates to the
Splitstrategy to compute each participant’s share - Strategy validates the split (amounts sum to total? percentages sum to 100?)
- For each participant (excluding payer): a debt is recorded in the BalanceSheet
- BalanceSheet updates the net balance between payer and each participant
- All registered BalanceObservers are notified of the change
- Lock is released, expense is stored
When a user triggers debt simplification:
- BalanceSheet computes the net balance for each user (total owed - total lent)
- Users with positive net balance go into a max-heap (creditors)
- Users with negative net balance go into a min-heap (debtors)
- Greedy: pop the largest creditor and largest debtor, settle the minimum of the two amounts
💡 Greedy = take the best-looking move available at each step and never reconsider it. It is safe here because settling the smaller of the two amounts always zeroes out at least one of the two people, so the problem shrinks every round. - Push back any remainder, repeat until both heaps are empty
- Result: the minimum number of transactions to settle all debts
💡 This greedy approach works because we only care about net flows, not individual expense history. If A owes B $10 and B owes A $3, the net is A→B $7.
Complete Code
User
User is an immutable value object representing a person in the system. Equality is based on id so the same user across different groups is recognized as identical.
// Money is held as a whole number of cents -- never as a double, which cannot
// represent 0.01 exactly, so shares stop adding back to the total.
final class Money {
private Money() {}
static final long CENTS_PER_UNIT = 100L;
static long units(long u) { return u * CENTS_PER_UNIT; }
/** Parse "10.00" into cents, exactly. */
static long parse(String text) {
String t = text.trim();
boolean neg = t.startsWith("-");
if (neg || t.startsWith("+")) t = t.substring(1);
int dot = t.indexOf('.');
long whole = Long.parseLong(dot < 0 ? t : t.substring(0, dot));
long frac = dot < 0 ? 0 : Long.parseLong((t.substring(dot + 1) + "00").substring(0, 2));
long v = whole * CENTS_PER_UNIT + frac;
return neg ? -v : v;
}
static String format(long cents) {
long a = Math.abs(cents);
return (cents < 0 ? "-" : "") + (a / CENTS_PER_UNIT)
+ String.format(".%02d", a % CENTS_PER_UNIT);
}
}
package splitwise.model;
public class User {
private final String id;
private final String name;
private final String email;
public User(String id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
public String getId() { return id; }
public String getName() { return name; }
public String getEmail() { return email; }
@Override
public String toString() { return name + " (" + id + ")"; }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
return id.equals(((User) o).id);
}
@Override
public int hashCode() { return id.hashCode(); }
}
CENTS_PER_UNIT = 100
def money_units(units: int) -> int:
"""Whole currency units to cents. Money never lives in a float: it cannot
hold 0.01 exactly, so shares stop adding back to the total."""
return units * CENTS_PER_UNIT
def money_format(cents: int) -> str:
sign = "-" if cents < 0 else ""
cents = abs(cents)
return f"{sign}{cents // CENTS_PER_UNIT}.{cents % CENTS_PER_UNIT:02d}"
from dataclasses import dataclass
@dataclass(frozen=True)
class User:
id: str
name: str
email: str
def __str__(self):
return f"{self.name} ({self.id})"
// Money is held as a whole number of cents -- never as a double, which cannot
// represent 0.01 exactly, so shares stop adding back to the total.
namespace Money {
constexpr long long CENTS_PER_UNIT = 100;
inline long long units(long long u) { return u * CENTS_PER_UNIT; }
inline std::string format(long long cents) {
long long a = cents < 0 ? -cents : cents;
std::ostringstream oss;
if (cents < 0) oss << "-";
oss << (a / CENTS_PER_UNIT) << "."
<< std::setw(2) << std::setfill('0') << (a % CENTS_PER_UNIT);
return oss.str();
}
}
#pragma once
#include <string>
#include <iostream>
class User {
public:
std::string id;
std::string name;
std::string email;
User(std::string id, std::string name, std::string email)
: id(std::move(id)), name(std::move(name)), email(std::move(email)) {}
bool operator==(const User& other) const { return id == other.id; }
bool operator!=(const User& other) const { return !(*this == other); }
friend std::ostream& operator<<(std::ostream& os, const User& u) {
os << u.name << " (" << u.id << ")";
return os;
}
};
Transaction
A simple value object representing a settlement - one user pays another a specific amount. Used as the output of debt simplification.
package splitwise.model;
public class Transaction {
private final User from;
private final User to;
private final long amountCents;
public Transaction(User from, User to, long amountCents) {
this.from = from;
this.to = to;
this.amountCents = amountCents;
}
public User getFrom() { return from; }
public User getTo() { return to; }
public long getAmountCents() { return amountCents; }
@Override
public String toString() {
return from.getName() + " pays " + to.getName() + ": $" + Money.format(amountCents);
}
}
from dataclasses import dataclass
@dataclass(frozen=True)
class Transaction:
from_user: User
to_user: User
amount_cents: int
def __str__(self):
return (f"{self.from_user.name} pays {self.to_user.name}: "
f"${money_format(self.amount_cents)}")
#pragma once
#include "User.h"
class Transaction {
public:
User from;
User to;
long long amountCents;
Transaction(User from, User to, long long amountCents)
: from(std::move(from)), to(std::move(to)), amountCents(amountCents) {}
friend std::ostream& operator<<(std::ostream& os, const Transaction& t) {
os << t.from.name << " pays " << t.to.name << ": $"
<< Money::format(t.amountCents);
return os;
}
};
Split (Strategy Interface)
This is the strategy interface that defines how an expense is divided among participants. EqualSplit, ExactSplit, and PercentageSplit are three implementations - but you could add WeightedSplit or ShareSplit without touching any existing code.
💡 Strategy pattern = define a family of algorithms, encapsulate each one, and make them interchangeable. Each expense picks its own split strategy at creation time. Zero if/else branching in ExpenseManager.
package splitwise.strategy;
import splitwise.model.User;
import java.util.List;
import java.util.Map;
public interface Split {
boolean validate(long totalCents, List<User> participants);
/** Shares in cents. They must sum exactly to totalCents. */
Map<User, Long> computeShares(long totalCents, List<User> participants);
}
from abc import ABC, abstractmethod
class Split(ABC):
@abstractmethod
def validate(self, total_cents: int, participants: list[User]) -> bool:
pass
@abstractmethod
def compute_shares(self, total_cents: int, participants: list[User]) -> dict[User, int]:
pass
#pragma once
#include <vector>
#include <unordered_map>
#include "User.h"
struct UserHash {
size_t operator()(const User& u) const {
return std::hash<std::string>{}(u.id);
}
};
class Split {
public:
virtual ~Split() = default;
virtual bool validate(long long totalCents, const std::vector<User>& participants) = 0;
/** Shares in cents. They must sum exactly to totalCents. */
virtual std::unordered_map<User, long long, UserHash> computeShares(
long long totalCents, const std::vector<User>& participants) = 0;
};
EqualSplit
The simplest strategy - divides the total equally among all participants. Validation just checks that there’s at least one participant.
package splitwise.strategy;
import splitwise.model.User;
import java.util.*;
public class EqualSplit implements Split {
@Override
public boolean validate(long totalCents, List<User> participants) {
return participants != null && !participants.isEmpty() && totalCents > 0;
}
/**
* Allocates in whole cents.
*
* Dividing the total by n fails this design's own correctness requirement
* twice over. 10.00 / 3 is 3.3333333333333335, which is not an amount anybody
* can pay; and three of those do not reliably add back to 10.00, because binary
* floating point cannot hold 0.01 exactly.
*
* So: divide as integers and hand out the remainder one cent at a time in
* participant order. 1000 cents over 3 becomes 334, 333, 333.
* Every share is payable, the allocation is deterministic, and the largest and
* smallest differ by at most a cent. Who absorbs the extra cent is a product
* decision -- the first participant here, often the payer in real systems -- but
* it has to be somebody, and silently losing it is not an option.
*
* LinkedHashMap rather than HashMap, so the remainder lands predictably instead of
* on whoever the hash order happens to favour.
*/
@Override
public Map<User, Long> computeShares(long totalCents, List<User> participants) {
int n = participants.size();
long base = totalCents / n;
long remainder = totalCents - base * n; // 0 <= remainder < n
Map<User, Long> shares = new LinkedHashMap<>();
int i = 0;
for (User u : participants) {
shares.put(u, base + (i < remainder ? 1 : 0));
i++;
}
return shares;
}
}
class EqualSplit(Split):
def validate(self, total_cents: int, participants: list[User]) -> bool:
return len(participants) > 0 and total_cents > 0
def compute_shares(self, total_cents: int, participants: list[User]) -> dict[User, int]:
# Allocate in whole cents. Dividing the total by n gives 3.3333333333333335 for 10/3,
# which is not payable and does not sum back to the total. Divide as integers
# and hand the remainder out a cent at a time, in participant order.
n = len(participants)
base, remainder = divmod(total_cents, n)
return {u: base + (1 if i < remainder else 0)
for i, u in enumerate(participants)}
#pragma once
#include "Split.h"
class EqualSplit : public Split {
public:
bool validate(long long totalCents, const std::vector<User>& participants) override {
return !participants.empty() && totalCents > 0;
}
// Allocate in whole cents. Dividing the total by n gives 3.3333333333333335 for 10/3,
// which is not payable and does not sum back to the total. Divide as integers and
// hand the remainder out a cent at a time, in participant order.
std::unordered_map<User, long long, UserHash> computeShares(
long long totalCents, const std::vector<User>& participants) override {
std::unordered_map<User, long long, UserHash> shares;
const long long n = static_cast<long long>(participants.size());
const long long base = totalCents / n;
const long long remainder = totalCents - base * n; // 0 <= remainder < n
for (long long i = 0; i < n; ++i) {
shares[participants[i]] = base + (i < remainder ? 1 : 0);
}
return shares;
}
};
ExactSplit
Each participant’s share is specified explicitly. Validation ensures the individual amounts sum exactly to the total expense. This is useful when items have different prices (e.g., everyone ordered different dishes).
package splitwise.strategy;
import splitwise.model.User;
import java.util.*;
public class ExactSplit implements Split {
private final Map<User, Long> exactCents;
public ExactSplit(Map<User, Long> exactCents) {
this.exactCents = exactCents;
}
@Override
public boolean validate(long totalCents, List<User> participants) {
if (exactCents == null || exactCents.isEmpty()) return false;
long sum = exactCents.values().stream().mapToLong(Long::longValue).sum();
// Exact equality, not a tolerance. With cents there is nothing to tolerate.
return sum == totalCents;
}
@Override
public Map<User, Long> computeShares(long totalCents, List<User> participants) {
return new LinkedHashMap<>(exactCents);
}
}
class ExactSplit(Split):
def __init__(self, exact_cents: dict[User, int]):
self._exact_cents = exact_cents
def validate(self, total_cents: int, participants: list[User]) -> bool:
if not self._exact_cents:
return False
# Exact equality, not a tolerance. With cents there is nothing to tolerate.
return sum(self._exact_cents.values()) == total_cents
def compute_shares(self, total_cents: int, participants: list[User]) -> dict[User, int]:
return dict(self._exact_cents)
#pragma once
#include "Split.h"
#include <cmath>
class ExactSplit : public Split {
std::unordered_map<User, long long, UserHash> exactCents;
public:
ExactSplit(std::unordered_map<User, long long, UserHash> amounts)
: exactCents(std::move(amounts)) {}
bool validate(long long totalCents, const std::vector<User>& participants) override {
if (exactCents.empty()) return false;
long long sum = 0;
for (const auto& [user, amt] : exactCents) sum += amt;
// Exact equality, not a tolerance. With cents there is nothing to tolerate.
return sum == totalCents;
}
std::unordered_map<User, long long, UserHash> computeShares(
long long totalCents, const std::vector<User>& participants) override {
return exactCents;
}
};
PercentageSplit
Each participant is assigned a percentage of the total. Validation checks that all percentages sum to 100. Useful for unequal splits like rent based on room size.
package splitwise.strategy;
import splitwise.model.User;
import java.util.*;
public class PercentageSplit implements Split {
// Whole percentages, summing to 100.
private final Map<User, Long> percentages;
public PercentageSplit(Map<User, Long> percentages) {
this.percentages = percentages;
}
@Override
public boolean validate(long totalCents, List<User> participants) {
if (percentages == null || percentages.isEmpty()) return false;
long sum = percentages.values().stream().mapToLong(Long::longValue).sum();
return sum == 100 && totalCents > 0;
}
@Override
public Map<User, Long> computeShares(long totalCents, List<User> participants) {
// Floor every share, then hand the leftover cents to the largest
// remainders. Multiplying a double by a percentage and hoping, as this
// did before, leaves the shares not summing to the total.
Map<User, Long> shares = new LinkedHashMap<>();
Map<User, Long> remainders = new LinkedHashMap<>();
long allocated = 0;
for (Map.Entry<User, Long> entry : percentages.entrySet()) {
long numerator = totalCents * entry.getValue();
long share = numerator / 100;
shares.put(entry.getKey(), share);
remainders.put(entry.getKey(), numerator % 100);
allocated += share;
}
long leftover = totalCents - allocated;
List<User> byRemainder = new ArrayList<>(remainders.keySet());
byRemainder.sort((a, b) -> Long.compare(remainders.get(b), remainders.get(a)));
for (int i = 0; i < leftover; i++) {
User u = byRemainder.get(i % byRemainder.size());
shares.put(u, shares.get(u) + 1);
}
return shares;
}
}
class PercentageSplit(Split):
# Whole percentages, summing to 100.
def __init__(self, percentages: dict[User, int]):
self._percentages = percentages
def validate(self, total_cents: int, participants: list[User]) -> bool:
if not self._percentages:
return False
return sum(self._percentages.values()) == 100 and total_cents > 0
def compute_shares(self, total_cents: int, participants: list[User]) -> dict[User, int]:
# Floor every share, then hand the leftover cents to the largest
# remainders, so the shares sum exactly to the total. Multiplying by a
# percentage and hoping, as this did before, does not.
shares: dict[User, int] = {}
remainders: dict[User, int] = {}
allocated = 0
for u, pct in self._percentages.items():
numerator = total_cents * pct
shares[u] = numerator // 100
remainders[u] = numerator % 100
allocated += shares[u]
leftover = total_cents - allocated
by_remainder = sorted(remainders, key=lambda u: -remainders[u])
for i in range(leftover):
shares[by_remainder[i % len(by_remainder)]] += 1
return shares
#pragma once
#include "Split.h"
#include <cmath>
class PercentageSplit : public Split {
// Whole percentages, summing to 100.
std::unordered_map<User, long long, UserHash> percentages;
public:
PercentageSplit(std::unordered_map<User, long long, UserHash> pcts)
: percentages(std::move(pcts)) {}
bool validate(long long totalCents, const std::vector<User>& participants) override {
if (percentages.empty()) return false;
long long sum = 0;
for (const auto& [user, pct] : percentages) sum += pct;
return sum == 100 && totalCents > 0;
}
std::unordered_map<User, long long, UserHash> computeShares(
long long totalCents, const std::vector<User>& participants) override {
std::unordered_map<User, long long, UserHash> shares;
for (const auto& [user, pct] : percentages) {
shares[user] = totalCents * pct / 100;
}
return shares;
}
};
Expense
An expense records who paid, how much, for what, and how it’s split. It holds a reference to its Split strategy and the list of participants. The expense is immutable once created.
package splitwise.model;
import splitwise.strategy.Split;
import java.util.*;
public class Expense {
private final String id;
private final long amountCents;
private final User payer;
private final String description;
private final Split splitStrategy;
private final List<User> participants;
public Expense(String id, long amountCents, User payer, String description,
Split splitStrategy, List<User> participants) {
this.id = id;
this.amountCents = amountCents;
this.payer = payer;
this.description = description;
this.splitStrategy = splitStrategy;
this.participants = Collections.unmodifiableList(participants);
}
public String getId() { return id; }
public long getAmountCents() { return amountCents; }
public User getPayer() { return payer; }
public String getDescription() { return description; }
public Split getSplitStrategy() { return splitStrategy; }
public List<User> getParticipants() { return participants; }
@Override
public String toString() {
return description + " - $" + Money.format(amountCents) + " paid by " + payer.getName();
}
}
class Expense:
def __init__(self, id: str, amount_cents: int, payer: User,
description: str, split_strategy: Split, participants: list[User]):
self.id = id
self.amount_cents = amount_cents
self.payer = payer
self.description = description
self.split_strategy = split_strategy
self.participants = list(participants)
def __str__(self):
return (f"{self.description} - ${money_format(self.amount_cents)} "
f"paid by {self.payer.name}")
#pragma once
#include <string>
#include <vector>
#include <memory>
#include "User.h"
#include "Split.h"
class Expense {
public:
std::string id;
long long amountCents;
User payer;
std::string description;
std::shared_ptr<Split> splitStrategy;
std::vector<User> participants;
Expense(std::string id, long long amountCents, User payer, std::string description,
std::shared_ptr<Split> strategy, std::vector<User> participants)
: id(std::move(id)), amountCents(amountCents), payer(std::move(payer)),
description(std::move(description)), splitStrategy(std::move(strategy)),
participants(std::move(participants)) {}
friend std::ostream& operator<<(std::ostream& os, const Expense& e) {
os << e.description << " - $" << Money::format(e.amountCents)
<< " paid by " << e.payer.name;
return os;
}
};
Group
A group holds a set of users and their shared expenses - like a trip or an apartment. It provides a scoped view of expenses for that context.
package splitwise.model;
import java.util.*;
public class Group {
private final String id;
private final String name;
private final List<User> members;
private final List<Expense> expenses;
public Group(String id, String name, List<User> members) {
this.id = id;
this.name = name;
this.members = new ArrayList<>(members);
this.expenses = new ArrayList<>();
}
public void addExpense(Expense expense) { expenses.add(expense); }
public String getId() { return id; }
public String getName() { return name; }
public List<User> getMembers() { return Collections.unmodifiableList(members); }
public List<Expense> getExpenses() { return Collections.unmodifiableList(expenses); }
@Override
public String toString() { return name + " (" + members.size() + " members)"; }
}
class Group:
def __init__(self, id: str, name: str, members: list[User]):
self.id = id
self.name = name
self.members = list(members)
self.expenses: list[Expense] = []
def add_expense(self, expense: Expense):
self.expenses.append(expense)
def __str__(self):
return f"{self.name} ({len(self.members)} members)"
#pragma once
#include <string>
#include <vector>
#include "User.h"
#include "Expense.h"
class Group {
public:
std::string id;
std::string name;
std::vector<User> members;
std::vector<Expense> expenses;
Group(std::string id, std::string name, std::vector<User> members)
: id(std::move(id)), name(std::move(name)), members(std::move(members)) {}
void addExpense(const Expense& expense) { expenses.push_back(expense); }
friend std::ostream& operator<<(std::ostream& os, const Group& g) {
os << g.name << " (" << g.members.size() << " members)";
return os;
}
};
BalanceSheet
The core accounting engine. Tracks net balances between every pair of users using a nested map: balances[A][B] = X means A owes B $X. When a new debt is recorded, it automatically nets against any existing reverse debt (if B already owed A, it reduces that first).
💡 A nested map is a map whose values are themselves maps, so this is a two-level lookup — first by who owes, then by who is owed. Both levels are hash lookups, so reading any single pairwise balance stays O(1).
💡 We store directed net balances rather than individual transactions. This means getBalance(userId) is O(n) where n = number of users, not number of expenses. The tradeoff: we lose individual expense history in the balance sheet (but Expense objects are stored separately).
The debt simplification algorithm uses a greedy approach with heaps:
- Compute net balance for each user (positive = creditor, negative = debtor)
- Push creditors into a max-heap, debtors into a min-heap
- Match the largest creditor with the largest debtor, settle the minimum
- Repeat until all settled - this minimizes total number of transactions
package splitwise.core;
import splitwise.model.Transaction;
import splitwise.model.User;
import java.util.*;
public class BalanceSheet {
// balances[A_id][B_id] = amount A owes B (positive means A owes B)
private final Map<String, Map<String, Long>> balances = new HashMap<>();
private final Map<String, User> userMap = new HashMap<>();
public void recordDebt(User from, User to, long amountCents) {
if (amountCents <= 0 || from.equals(to)) return;
userMap.putIfAbsent(from.getId(), from);
userMap.putIfAbsent(to.getId(), to);
// Net the balances: if to already owes from, reduce that first
long reverseDebt = getDirectDebt(to.getId(), from.getId());
if (reverseDebt >= amountCents) {
setDebt(to.getId(), from.getId(), reverseDebt - amountCents);
} else {
setDebt(to.getId(), from.getId(), 0);
long existing = getDirectDebt(from.getId(), to.getId());
setDebt(from.getId(), to.getId(), existing + (amountCents - reverseDebt));
}
}
public Map<String, Long> getBalanceForUser(String userId) {
Map<String, Long> result = new HashMap<>();
// What this user owes others (negative for them)
Map<String, Long> owes = balances.getOrDefault(userId, Collections.emptyMap());
for (Map.Entry<String, Long> e : owes.entrySet()) {
if (e.getValue() > 0.01) result.put(e.getKey(), -e.getValue());
}
// What others owe this user (positive for them)
for (Map.Entry<String, Map<String, Long>> entry : balances.entrySet()) {
if (entry.getKey().equals(userId)) continue;
long amt = entry.getValue().getOrDefault(userId, 0L);
if (amt > 0.01) result.put(entry.getKey(), amt);
}
return result;
}
public List<Transaction> simplifyDebts() {
// Step 1: Compute net balance for each user
Map<String, Long> netBalance = new HashMap<>();
for (Map.Entry<String, Map<String, Long>> entry : balances.entrySet()) {
String fromId = entry.getKey();
for (Map.Entry<String, Long> debt : entry.getValue().entrySet()) {
String toId = debt.getKey();
long amt = debt.getValue();
if (amt < 0.01) continue;
netBalance.merge(fromId, -amt, Long::sum); // from owes, so negative
netBalance.merge(toId, amt, Long::sum); // to is owed, so positive
}
}
// Step 2: Separate into creditors (positive) and debtors (negative)
// Max-heap for creditors, min-heap (max of abs) for debtors
PriorityQueue<long[]> creditors = new PriorityQueue<>((a, b) -> Long.compare(b[1], a[1]));
PriorityQueue<long[]> debtors = new PriorityQueue<>((a, b) -> Long.compare(a[1], b[1]));
List<String> userIds = new ArrayList<>(netBalance.keySet());
for (String uid : userIds) {
long bal = netBalance.get(uid);
// Exact tests against zero. The old 0.01 epsilon was standing in for
// "close enough to zero", which integer cents make unnecessary.
if (bal > 0) creditors.offer(new long[]{userIds.indexOf(uid), bal});
else if (bal < 0) debtors.offer(new long[]{userIds.indexOf(uid), bal});
}
// Step 3: Greedy matching
List<Transaction> transactions = new ArrayList<>();
while (!creditors.isEmpty() && !debtors.isEmpty()) {
long[] creditor = creditors.poll();
long[] debtor = debtors.poll();
long settle = Math.min(creditor[1], -debtor[1]);
User fromUser = userMap.get(userIds.get((int) debtor[0]));
User toUser = userMap.get(userIds.get((int) creditor[0]));
transactions.add(new Transaction(fromUser, toUser, settle));
creditor[1] -= settle;
debtor[1] += settle;
if (creditor[1] > 0.01) creditors.offer(creditor);
if (debtor[1] < -0.01) debtors.offer(debtor);
}
return transactions;
}
private long getDirectDebt(String fromId, String toId) {
return balances.getOrDefault(fromId, Collections.emptyMap()).getOrDefault(toId, 0L);
}
private void setDebt(String fromId, String toId, long amountCents) {
balances.computeIfAbsent(fromId, k -> new HashMap<>()).put(toId, amountCents);
}
}
import heapq
from collections import defaultdict
class BalanceSheet:
def __init__(self):
# balances[from_id][to_id] = amount from owes to
self._balances: dict[str, dict[str, int]] = defaultdict(lambda: defaultdict(int))
self._user_map: dict[str, User] = {}
def record_debt(self, from_user: User, to_user: User, amount_cents: int):
if amount_cents <= 0 or from_user == to_user:
return
self._user_map[from_user.id] = from_user
self._user_map[to_user.id] = to_user
# Net against reverse debt
reverse = self._balances[to_user.id][from_user.id]
if reverse >= amount_cents:
self._balances[to_user.id][from_user.id] = reverse - amount_cents
else:
self._balances[to_user.id][from_user.id] = 0
self._balances[from_user.id][to_user.id] += (amount_cents - reverse)
def get_balance_for_user(self, user_id: str) -> dict[str, int]:
result = {}
# What this user owes others
for to_id, amt in self._balances[user_id].items():
if amt > 0:
result[to_id] = -amt
# What others owe this user
for from_id, debts in self._balances.items():
if from_id == user_id:
continue
amt = debts.get(user_id, 0)
if amt > 0:
result[from_id] = amt
return result
def simplify_debts(self) -> list[Transaction]:
# Step 1: Net balances
net: dict[str, int] = defaultdict(int)
for from_id, debts in self._balances.items():
for to_id, amt in debts.items():
if amt < 0:
continue
net[from_id] -= amt
net[to_id] += amt
# Step 2: Separate creditors and debtors
# Max-heap for creditors (negate for heapq), min-heap for debtors
creditors = [] # (-amount, user_id) - max-heap via negation
debtors = [] # (amount, user_id) - min-heap (most negative first)
for uid, bal in net.items():
if bal > 0:
heapq.heappush(creditors, (-bal, uid))
elif bal < 0:
heapq.heappush(debtors, (bal, uid))
# Step 3: Greedy matching
transactions = []
while creditors and debtors:
credit_amt, credit_id = heapq.heappop(creditors)
debt_amt, debt_id = heapq.heappop(debtors)
settle = min(-credit_amt, -debt_amt)
from_user = self._user_map[debt_id]
to_user = self._user_map[credit_id]
transactions.append(Transaction(from_user, to_user, round(settle, 2)))
remainder_credit = -credit_amt - settle
remainder_debt = -debt_amt - settle
if remainder_credit > 0:
heapq.heappush(creditors, (-remainder_credit, credit_id))
if remainder_debt > 0:
heapq.heappush(debtors, (-remainder_debt, debt_id))
return transactions
#pragma once
#include <unordered_map>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include "User.h"
#include "Transaction.h"
class BalanceSheet {
// balances[fromId][toId] = amount from owes to
std::unordered_map<std::string, std::unordered_map<std::string, long long>> balances;
std::unordered_map<std::string, User> userMap;
long long getDirectDebt(const std::string& fromId, const std::string& toId) {
auto it = balances.find(fromId);
if (it == balances.end()) return 0;
auto it2 = it->second.find(toId);
return it2 == it->second.end() ? 0 : it2->second;
}
void setDebt(const std::string& fromId, const std::string& toId, long long amountCents) {
balances[fromId][toId] = amountCents;
}
public:
void recordDebt(const User& from, const User& to, long long amountCents) {
if (amountCents <= 0 || from == to) return;
userMap.emplace(from.id, from);
userMap.emplace(to.id, to);
long long reverse = getDirectDebt(to.id, from.id);
if (reverse >= amountCents) {
setDebt(to.id, from.id, reverse - amountCents);
} else {
setDebt(to.id, from.id, 0);
long long existing = getDirectDebt(from.id, to.id);
setDebt(from.id, to.id, existing + (amountCents - reverse));
}
}
std::vector<Transaction> simplifyDebts() {
// Net balances
std::unordered_map<std::string, double> net;
for (auto& [fromId, debts] : balances) {
for (auto& [toId, amt] : debts) {
if (amt == 0) continue;
net[fromId] -= amt;
net[toId] += amt;
}
}
// Max-heap for creditors, max-heap (by abs) for debtors
using Pair = std::pair<double, std::string>;
std::priority_queue<Pair> creditors;
std::priority_queue<Pair> debtors;
for (auto& [uid, bal] : net) {
if (bal > 0) creditors.push({bal, uid});
else if (bal < 0) debtors.push({-bal, uid});
}
std::vector<Transaction> transactions;
while (!creditors.empty() && !debtors.empty()) {
auto [creditAmt, creditId] = creditors.top(); creditors.pop();
auto [debtAmt, debtId] = debtors.top(); debtors.pop();
double settle = std::min(creditAmt, debtAmt);
transactions.emplace_back(userMap.at(debtId), userMap.at(creditId), settle);
if (creditAmt - settle > 0) creditors.push({creditAmt - settle, creditId});
if (debtAmt - settle > 0) debtors.push({debtAmt - settle, debtId});
}
return transactions;
}
};
BalanceObserver
Observer interface for balance change notifications. UI components, notification services, or logging can implement this to react when debts change - without the core system knowing about them.
💡 Observer pattern = decouple the “what happened” from “who cares.” ExpenseManager fires events; any number of observers can listen without modifying the manager. Learn more →
package splitwise.observer;
import splitwise.model.Expense;
public interface BalanceObserver {
void onExpenseAdded(Expense expense);
void onBalanceChanged(String userId, long newNetBalanceCents);
}
class BalanceObserver:
def on_expense_added(self, expense: Expense):
pass
def on_balance_changed(self, user_id: str, new_net_balance_cents: int):
pass
#pragma once
#include <string>
#include "Expense.h"
class BalanceObserver {
public:
virtual ~BalanceObserver() = default;
virtual void onExpenseAdded(const Expense& expense) = 0;
virtual void onBalanceChanged(const std::string& userId,
long long newNetBalanceCents) = 0;
};
ExpenseManager (Facade)
The single entry point for all operations. It orchestrates user registration, group creation, expense addition, balance queries, and debt simplification. All public methods are thread-safe via ReentrantLock.
💡 ReentrantLock is an explicit lock object you take and release yourself, unlike the synchronized keyword which does it implicitly around a block. “Reentrant” means the thread already holding it can take it again without deadlocking itself — which is what lets addExpense() safely call another locked method.
💡 Facade pattern = provide a simplified interface to a complex subsystem. Clients call addExpense() on the manager - they never interact directly with BalanceSheet, Split strategies, or Groups.
package splitwise;
import splitwise.core.BalanceSheet;
import splitwise.model.*;
import splitwise.observer.BalanceObserver;
import splitwise.strategy.Split;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantLock;
public class ExpenseManager {
private final Map<String, User> users = new HashMap<>();
private final Map<String, Group> groups = new HashMap<>();
private final List<Expense> expenses = new ArrayList<>();
private final BalanceSheet balanceSheet = new BalanceSheet();
private final ReentrantLock lock = new ReentrantLock();
private final List<BalanceObserver> observers = new CopyOnWriteArrayList<>();
public void addUser(User user) {
lock.lock();
try {
users.put(user.getId(), user);
} finally { lock.unlock(); }
}
public void createGroup(String groupId, String name, List<User> members) {
lock.lock();
try {
Group group = new Group(groupId, name, members);
groups.put(groupId, group);
for (User u : members) users.putIfAbsent(u.getId(), u);
} finally { lock.unlock(); }
}
public void addExpense(String expenseId, long amountCents, User payer,
String description, Split splitStrategy,
List<User> participants, String groupId) {
// Declared out here so the observers can still see it once the lock
// has been released.
Expense expense;
lock.lock();
try {
if (!splitStrategy.validate(amountCents, participants)) {
throw new IllegalArgumentException("Invalid split: amounts do not match total");
}
expense = new Expense(expenseId, amountCents, payer, description,
splitStrategy, participants);
expenses.add(expense);
if (groupId != null && groups.containsKey(groupId)) {
groups.get(groupId).addExpense(expense);
}
// Compute shares and record debts
Map<User, Long> shares = splitStrategy.computeShares(amountCents, participants);
for (Map.Entry<User, Long> entry : shares.entrySet()) {
User participant = entry.getKey();
long share = entry.getValue();
if (!participant.equals(payer) && share > 0) {
balanceSheet.recordDebt(participant, payer, share);
}
}
} finally { lock.unlock(); }
// Observers run outside the lock. They are foreign code -- free to
// block, throw, or call the manager back -- and none of that should
// happen holding the balance-sheet lock.
for (BalanceObserver obs : observers) {
obs.onExpenseAdded(expense);
}
}
public Map<String, Long> getBalance(String userId) {
lock.lock();
try {
return balanceSheet.getBalanceForUser(userId);
} finally { lock.unlock(); }
}
public List<Transaction> simplifyDebts() {
lock.lock();
try {
return balanceSheet.simplifyDebts();
} finally { lock.unlock(); }
}
public void addObserver(BalanceObserver observer) {
observers.add(observer);
}
public Group getGroup(String groupId) {
return groups.get(groupId);
}
public List<Expense> getAllExpenses() {
return Collections.unmodifiableList(expenses);
}
}
import threading
class ExpenseManager:
def __init__(self):
self._users: dict[str, User] = {}
self._groups: dict[str, Group] = {}
self._expenses: list[Expense] = []
self._balance_sheet = BalanceSheet()
self._lock = threading.Lock()
self._observers: list[BalanceObserver] = []
def add_user(self, user: User):
with self._lock:
self._users[user.id] = user
def create_group(self, group_id: str, name: str, members: list[User]):
with self._lock:
group = Group(group_id, name, members)
self._groups[group_id] = group
for u in members:
self._users.setdefault(u.id, u)
def add_expense(self, expense_id: str, amount_cents: int, payer: User,
description: str, split_strategy: Split,
participants: list[User], group_id: str = None):
with self._lock:
if not split_strategy.validate(amount_cents, participants):
raise ValueError("Invalid split: amounts do not match total")
expense = Expense(expense_id, amount_cents, payer, description,
split_strategy, participants)
self._expenses.append(expense)
if group_id and group_id in self._groups:
self._groups[group_id].add_expense(expense)
# Compute shares and record debts
shares = split_strategy.compute_shares(amount_cents, participants)
for participant, share in shares.items():
if participant != payer and share > 0:
self._balance_sheet.record_debt(participant, payer, share)
# Observers run outside the lock. They are foreign code: an
# observer is free to block, raise, or call the manager back, and
# self._lock is not reentrant so that last one would hang.
for obs in self._observers:
obs.on_expense_added(expense)
def get_balance(self, user_id: str) -> dict[str, int]:
with self._lock:
return self._balance_sheet.get_balance_for_user(user_id)
def simplify_debts(self) -> list[Transaction]:
with self._lock:
return self._balance_sheet.simplify_debts()
def add_observer(self, observer: BalanceObserver):
self._observers.append(observer)
def get_group(self, group_id: str) -> Group:
return self._groups.get(group_id)
def get_all_expenses(self) -> list[Expense]:
return list(self._expenses)
#pragma once
#include <mutex>
#include <vector>
#include <unordered_map>
#include <memory>
#include <stdexcept>
#include "User.h"
#include "Group.h"
#include "Expense.h"
#include "BalanceSheet.h"
#include "BalanceObserver.h"
#include "Split.h"
class ExpenseManager {
std::unordered_map<std::string, User> users;
std::unordered_map<std::string, Group> groups;
std::vector<Expense> expenses;
BalanceSheet balanceSheet;
std::mutex mtx;
std::vector<BalanceObserver*> observers;
public:
void addUser(const User& user) {
std::lock_guard<std::mutex> lock(mtx);
users.emplace(user.id, user);
}
void createGroup(const std::string& groupId, const std::string& name,
const std::vector<User>& members) {
std::lock_guard<std::mutex> lock(mtx);
groups.emplace(groupId, Group(groupId, name, members));
for (const auto& u : members) users.emplace(u.id, u);
}
void addExpense(const std::string& expenseId, long long amountCents, const User& payer,
const std::string& description, std::shared_ptr<Split> splitStrategy,
const std::vector<User>& participants, const std::string& groupId = "") {
std::unique_lock<std::mutex> lock(mtx);
if (!splitStrategy->validate(amountCents, participants)) {
throw std::invalid_argument("Invalid split: amounts do not match total");
}
Expense expense(expenseId, amountCents, payer, description, splitStrategy, participants);
expenses.push_back(expense);
if (!groupId.empty()) {
auto it = groups.find(groupId);
if (it != groups.end()) it->second.addExpense(expense);
}
auto shares = splitStrategy->computeShares(amountCents, participants);
for (const auto& [participant, share] : shares) {
if (participant != payer && share > 0) {
balanceSheet.recordDebt(participant, payer, share);
}
}
lock.unlock();
// Observers run outside the lock. relocking a std::mutex this thread
// already holds is undefined behaviour, so an observer that called the
// manager back used to hang here.
for (auto* obs : observers) obs->onExpenseAdded(expense);
}
std::vector<Transaction> simplifyDebts() {
std::lock_guard<std::mutex> lock(mtx);
return balanceSheet.simplifyDebts();
}
void addObserver(BalanceObserver* obs) { observers.push_back(obs); }
};
Demo (Runnable)
The demo proves the entire system works end-to-end: creates users, forms a group, adds expenses with all three split types, shows individual balances, and runs debt simplification to minimize transactions.
package splitwise;
import splitwise.model.*;
import splitwise.observer.BalanceObserver;
import splitwise.strategy.*;
import java.util.*;
public class Demo {
public static void main(String[] args) {
System.out.println("══════ SPLITWISE DEMO ══════\n");
ExpenseManager manager = new ExpenseManager();
manager.addObserver(new BalanceObserver() {
public void onExpenseAdded(Expense e) {
System.out.println(" 📝 Expense added: " + e);
}
public void onBalanceChanged(String uid, long balCents) {}
});
// Create users
User alice = new User("u1", "Alice", "alice@email.com");
User bob = new User("u2", "Bob", "bob@email.com");
User charlie = new User("u3", "Charlie", "charlie@email.com");
User diana = new User("u4", "Diana", "diana@email.com");
manager.addUser(alice);
manager.addUser(bob);
manager.addUser(charlie);
manager.addUser(diana);
// Create a trip group
manager.createGroup("g1", "Goa Trip",
Arrays.asList(alice, bob, charlie, diana));
System.out.println("Group created: Goa Trip\n");
// --- EQUAL SPLIT ---
System.out.println("--- Equal Split: Dinner $240 paid by Alice ---");
manager.addExpense("e1", Money.units(240), alice, "Dinner",
new EqualSplit(),
Arrays.asList(alice, bob, charlie, diana), "g1");
// --- EXACT SPLIT ---
System.out.println("\n--- Exact Split: Shopping $500 paid by Bob ---");
Map<User, Long> exactAmounts = new LinkedHashMap<>();
exactAmounts.put(alice, Money.units(100));
exactAmounts.put(bob, Money.units(200));
exactAmounts.put(charlie, Money.units(50));
exactAmounts.put(diana, Money.units(150));
manager.addExpense("e2", Money.units(500), bob, "Shopping",
new ExactSplit(exactAmounts),
Arrays.asList(alice, bob, charlie, diana), "g1");
// --- PERCENTAGE SPLIT ---
System.out.println("\n--- Percentage Split: Hotel $1000 paid by Charlie ---");
Map<User, Long> percentages = new LinkedHashMap<>();
percentages.put(alice, 30L);
percentages.put(bob, 30L);
percentages.put(charlie, 20L);
percentages.put(diana, 20L);
manager.addExpense("e3", Money.units(1000), charlie, "Hotel",
new PercentageSplit(percentages),
Arrays.asList(alice, bob, charlie, diana), "g1");
// --- BALANCES ---
System.out.println("\n--- Balances ---");
for (User u : Arrays.asList(alice, bob, charlie, diana)) {
Map<String, Long> bal = manager.getBalance(u.getId());
System.out.println(" " + u.getName() + ":");
for (Map.Entry<String, Long> e : bal.entrySet()) {
String label = e.getValue() > 0 ? "is owed" : "owes";
System.out.println(" " + label + " $"
+ Money.format(Math.abs(e.getValue())) + " (user " + e.getKey() + ")");
}
}
// --- SIMPLIFY DEBTS ---
System.out.println("\n--- Simplified Debts (Minimum Transactions) ---");
List<Transaction> simplified = manager.simplifyDebts();
for (Transaction t : simplified) {
System.out.println(" " + t);
}
System.out.println(" Total transactions: " + simplified.size());
System.out.println("\n══════ DONE ══════");
}
}
def demo():
print("══════ SPLITWISE DEMO ══════\n")
class ConsolePrinter(BalanceObserver):
def on_expense_added(self, expense):
print(f" 📝 Expense added: {expense}")
def on_balance_changed(self, user_id, new_net_balance):
pass
manager = ExpenseManager()
manager.add_observer(ConsolePrinter())
# Create users
alice = User("u1", "Alice", "alice@email.com")
bob = User("u2", "Bob", "bob@email.com")
charlie = User("u3", "Charlie", "charlie@email.com")
diana = User("u4", "Diana", "diana@email.com")
manager.add_user(alice)
manager.add_user(bob)
manager.add_user(charlie)
manager.add_user(diana)
# Create a trip group
manager.create_group("g1", "Goa Trip", [alice, bob, charlie, diana])
print("Group created: Goa Trip\n")
# --- EQUAL SPLIT ---
print("--- Equal Split: Dinner $240 paid by Alice ---")
manager.add_expense("e1", money_units(240), alice, "Dinner",
EqualSplit(), [alice, bob, charlie, diana], "g1")
# --- EXACT SPLIT ---
print("\n--- Exact Split: Shopping $500 paid by Bob ---")
exact_cents = {alice: money_units(100), bob: money_units(200),
charlie: money_units(50), diana: money_units(150)}
manager.add_expense("e2", money_units(500), bob, "Shopping",
ExactSplit(exact_cents), [alice, bob, charlie, diana], "g1")
# --- PERCENTAGE SPLIT ---
print("\n--- Percentage Split: Hotel $1000 paid by Charlie ---")
percentages = {alice: 30, bob: 30, charlie: 20, diana: 20}
manager.add_expense("e3", money_units(1000), charlie, "Hotel",
PercentageSplit(percentages), [alice, bob, charlie, diana], "g1")
# --- BALANCES ---
print("\n--- Balances ---")
for u in [alice, bob, charlie, diana]:
bal = manager.get_balance(u.id)
print(f" {u.name}:")
for other_id, amount_cents in bal.items():
label = "is owed" if amount_cents > 0 else "owes"
print(f" {label} ${money_format(abs(amount_cents))} (user {other_id})")
# --- SIMPLIFY DEBTS ---
print("\n--- Simplified Debts (Minimum Transactions) ---")
simplified = manager.simplify_debts()
for t in simplified:
print(f" {t}")
print(f" Total transactions: {len(simplified)}")
print("\n══════ DONE ══════")
if __name__ == "__main__":
demo()
#include <iostream>
#include <vector>
#include <memory>
#include "ExpenseManager.h"
#include "EqualSplit.h"
#include "ExactSplit.h"
#include "PercentageSplit.h"
class ConsolePrinter : public BalanceObserver {
public:
void onExpenseAdded(const Expense& e) override {
std::cout << " 📝 Expense added: " << e << "\n";
}
void onBalanceChanged(const std::string& uid, long long balCents) override {}
};
int main() {
std::cout << "══════ SPLITWISE DEMO ══════\n\n";
ExpenseManager manager;
ConsolePrinter printer;
manager.addObserver(&printer);
// Create users
User alice("u1", "Alice", "alice@email.com");
User bob("u2", "Bob", "bob@email.com");
User charlie("u3", "Charlie", "charlie@email.com");
User diana("u4", "Diana", "diana@email.com");
manager.addUser(alice);
manager.addUser(bob);
manager.addUser(charlie);
manager.addUser(diana);
// Create group
manager.createGroup("g1", "Goa Trip", {alice, bob, charlie, diana});
std::cout << "Group created: Goa Trip\n\n";
// --- EQUAL SPLIT ---
std::cout << "--- Equal Split: Dinner $240 paid by Alice ---\n";
manager.addExpense("e1", Money::units(240), alice, "Dinner",
std::make_shared<EqualSplit>(),
{alice, bob, charlie, diana}, "g1");
// --- EXACT SPLIT ---
std::cout << "\n--- Exact Split: Shopping $500 paid by Bob ---\n";
std::unordered_map<User, long long, UserHash> exactAmounts = {
{alice, Money::units(100)}, {bob, Money::units(200)},
{charlie, Money::units(50)}, {diana, Money::units(150)}
};
manager.addExpense("e2", Money::units(500), bob, "Shopping",
std::make_shared<ExactSplit>(exactAmounts),
{alice, bob, charlie, diana}, "g1");
// --- PERCENTAGE SPLIT ---
std::cout << "\n--- Percentage Split: Hotel $1000 paid by Charlie ---\n";
std::unordered_map<User, long long, UserHash> pcts = {
{alice, 30.0}, {bob, 30.0}, {charlie, 20.0}, {diana, 20.0}
};
manager.addExpense("e3", Money::units(1000), charlie, "Hotel",
std::make_shared<PercentageSplit>(pcts),
{alice, bob, charlie, diana}, "g1");
// --- SIMPLIFY DEBTS ---
std::cout << "\n--- Simplified Debts (Minimum Transactions) ---\n";
auto simplified = manager.simplifyDebts();
for (const auto& t : simplified) {
std::cout << " " << t << "\n";
}
std::cout << " Total transactions: " << simplified.size() << "\n";
std::cout << "\n══════ DONE ══════\n";
return 0;
}
Debt Simplification - Algorithm Walkthrough
flowchart LR
A[Compute Net Balances] --> B[Separate Creditors and Debtors]
B --> C[Max-Heap Creditors]
B --> D[Min-Heap Debtors]
C --> E[Greedy: Match Largest Creditor with Largest Debtor]
D --> E
E --> F[Settle min of both amounts]
F --> G{Both heaps empty?}
G -->|No| E
G -->|Yes| H[Return Minimal Transactions]
Example: Alice is owed $100, Bob is owed $50, Charlie owes $80, Diana owes $70.
- Match Alice ($100) with Diana ($70) → Diana pays Alice $70. Alice remainder: $30.
- Match Bob ($50) with Charlie ($80) → Charlie pays Bob $50. Charlie remainder: $30.
- Match Alice ($30) with Charlie ($30) → Charlie pays Alice $30.
- Result: 3 transactions instead of potentially many more pairwise settlements.
How to Extend
| Feature | Implementation |
|---|---|
| Weighted Split | New WeightedSplit - divide proportionally by weight values |
| Settlement | Track settled transactions, reduce balances accordingly |
| Expense Editing | Reverse old splits, apply new splits atomically |
| Currency Support | Decorator on Split that converts amounts before computing |
| Recurring Expenses | Scheduler that calls addExpense() on a cron |
💡 Decorator = wrap an object in another object that exposes the same interface, so the wrapper can adjust what goes in or comes out while the original stays untouched. A currency decorator would convert the amount and then hand off to the real Split. Learn more →
What Interviewers Look For
- ✅ Strategy pattern for split types - no if/else chains
- ✅ Debt simplification - greedy with heaps, O(n log n)
- ✅ Thread-safety - locks on all mutable shared state
- ✅ Validation - splits must sum to total before recording
- ✅ Observer - decoupled notifications on balance changes
- ✅ Facade - ExpenseManager hides subsystem complexity
- ✅ Runnable demo - all three split types exercised end-to-end
Related Designs
- Music Player - Strategy and Observer patterns
- Parking Lot - Strategy-based pricing and extensible design
Related Concepts
Scale this design past a single process and these are the concepts it runs into:
- Transactions & Isolation Levels → — one expense updates several pairwise balances; a partial write leaves the balance sheet wrong
- Event Sourcing & CQRS → — the expense log is the source of truth and balances are a projection you can rebuild
- Idempotency → — a double-submitted expense must not be counted twice
- Caching → — balance summaries are read constantly and change rarely, which is the ideal cache shape
- Database Sharding → — sharding by group keeps every expense in a group on one node so the join stays local
Discussion
Newest first