Settle Group Expenses with Transfers
PinterestSoftware Engineer · Technical ScreenLast updated
This question evaluates the ability to compute and reconcile per-person financial balances from transactional data and to translate net liabilities into direct transfers, exercising skills in numeric bookkeeping and data aggregation.
You are given a list of group expense transactions from a trip.
Each transaction contains:
payer: the person who paid the full amount.amount: the total amount paid, represented as an integer number of cents.payees: the people who shared this expense equally. The payer may also appear in this list, meaning they paid for their own share as well.
For each transaction, the amount is split equally among all payees. Compute a list of direct transfers that settles all balances, so that every person's final net balance becomes zero.
Return any valid list of transfers. Each transfer should specify:
from: the person who pays money.to: the person who receives money.amount: the amount transferred.
The order of transfers does not matter. You may assume each transaction amount is evenly divisible by the number of payees.
Example:
transactions = [
{
"payer": "Alice",
"amount": 4000,
"payees": ["Bob", "Alice", "Charlie", "Daisy"]
},
{
"payer": "Charlie",
"amount": 2000,
"payees": ["Alice", "Charlie"]
}
]
One valid output is:
[
{"from": "Bob", "to": "Alice", "amount": 1000},
{"from": "Daisy", "to": "Alice", "amount": 1000}
]
Explanation:
- Alice should receive a net total of
2000. - Bob owes
1000. - Daisy owes
1000. - Charlie is already settled.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.