← Questions

Settle group trip expenses with the fewest transfers

0
GoogleCodingLast asked Nov 2025

A coding interview question asked in Google's first round (coding), last asked Nov 2025. It tests backtracking, greedy and hash maps.

Friends on a trip record shared expenses. Each expense names the payer, the amount in cents, and the payees who split it equally; the payer may be one of them. Return money transfers between friends that bring every person's balance to zero using as few transfers as possible.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • settle(expenses: list[dict]) -> list[dict] — each expense is {"payer": str, "amount": int, "payees": list[str]} and amount divides evenly by len(payees). Each transfer is {"payer": debtor, "amount": int, "payees": [creditor]} (exactly one receiver). Any minimal answer is accepted. Names are case-sensitive.

Example

settle([
  {"payer": "alice", "amount": 4000, "payees": ["bob", "jess", "alice", "sam"]},
  {"payer": "jess",  "amount": 2000, "payees": ["jess", "alice"]},
])
  ->  [{"payer": "bob", "amount": 1000, "payees": ["alice"]},
       {"payer": "sam", "amount": 1000, "payees": ["alice"]}]
# balances: alice +2000, jess 0, bob -1000, sam -1000

Matching debtors to creditors greedily is not always minimal. With balances ana +700, ben +300, cal -300, dee -700, pairing in name order gives three transfers (cal to ana 300, dee to ana 400, dee to ben 300), but two suffice (cal to ben 300, dee to ana 700).

Constraints

  • Amounts are integer cents. Balances always sum to zero.
  • Many expenses, but usually a small number of people with non-zero balances.

Clarifying questions

  • Is "minimal" the fewest transfers, or is a simple, near-minimal answer good enough?
  • May the payer be among the payees?
  • Do transfers have to follow who actually owes whom, or only net balances?
  • How many people can there be?

What a strong answer covers

  • Reduce everything to one net balance per person: the payer gains amount, each payee loses amount / len(payees). Drop zeros.
  • Greedy matching of debtors and creditors (largest first with two heaps) settles in at most k - 1 transfers for k non-zero people, but is not always optimal.
  • The exact answer: k minus the largest number of disjoint groups whose balances sum to zero. Backtracking or a DP over subsets, O(2^k · k), fine for up to about 20 people.
  • Why the problem is NP-hard in general, and when to choose greedy.
  • Tests: everyone even, one payer for all, the counterexample above.

Follow-ups

  • Amounts don't divide evenly. How do you assign the leftover cents fairly?
  • Expenses use different currencies.
  • Some people refuse to pay certain others; transfers must stay within allowed pairs.

Where it's asked

  • Google · First round: coding · Last asked Nov 202545 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI

What it tests

backtracking · greedy · hash maps

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding