KatInterview

Settle Shared Expenses With a Two-Pointer Payment Plan

Greedy & IntervalsAuto-graded · 21 test cases
EasyAuto-gradedGreedy & Intervalsgreedytwo-pointershash-map

Last updated

Turn a map of net balances into a deterministic list of payments that zeroes every account, using a sorted two-pointer greedy match between debtors and creditors.

Settle Up: Build the Payment Plan

A group of accounts has tracked shared spending. You are given a dictionary net_balances that maps each account name (a string) to an integer net balance.

  • A negative value means the account owes money and must pay.
  • A positive value means the account is owed money and must receive.
  • A value of 0 means the account is already settled.
  • The sum of all balances is exactly 0.

Return a list of payments that brings every account to a net balance of 0.

Output format

Each payment is a list [payer, receiver, amount], where amount is a positive integer. The function returns a list of such payments.

Required procedure

Because the checker compares your output exactly, build the payments in this deterministic way:

  1. Collect the debtors (negative balances), recording each one's debt as a positive number. Sort them by account name in ascending (standard string) order.
  2. Collect the creditors (positive balances) and sort them by account name in ascending order.
  3. Ignore accounts with a balance of 0.
  4. Keep a pointer at the first debtor and the first creditor. Repeatedly:
    • Let amount be the smaller of the current debtor's remaining debt and the current creditor's remaining credit.
    • Append [debtor_name, creditor_name, amount] to the result.
    • Subtract amount from both remaining values.
    • Whichever side reaches 0 moves on to the next account in its list (both move on if both reach 0). A side with a remainder stays as the current one with its reduced value.
  5. Stop when either list is exhausted. By the zero-sum guarantee, both will run out together.

If there is nothing to settle (empty input, or all balances zero), return an empty list.

Constraints

  • 0 <= len(net_balances) <= 10^5
  • Account names are distinct strings.
  • Balances are integers, and their total is exactly 0.
  • Every payment in the output has amount > 0, and no account pays itself.
  • The output must follow the procedure above (sorted order, greedy matching).

Examples

Example 1

Input: settle_up({'A': -5, 'B': 2, 'C': 3})

Output: [['A', 'B', 2], ['A', 'C', 3]]

Example 2

Input: settle_up({'alice': -10, 'bob': 10})

Output: [['alice', 'bob', 10]]

Example 3

Input: settle_up({'A': -7, 'B': -3, 'C': 4, 'D': 6})

Output: [['A', 'C', 4], ['A', 'D', 3], ['B', 'D', 3]]

Hints

Hint 1

Split the accounts into two groups, those who owe and those who are owed, and store debts as positive numbers so both groups look alike. Skip zeros.

Hint 2

Sort each group by account name, then walk both lists with two indices. At each step move min(remaining debt, remaining credit).

Hint 3

After each payment, advance the index of any side that reached zero and keep the reduced remainder for the side that did not. Never emit a zero-amount payment.