How to settle multi-person debts in minimum transactions
Company: Stripe
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithm design and combinatorial optimization skills, focusing on reasoning about net balances, transaction minimization, and efficient search in multi-party debt settlement within the Coding & Algorithms domain.
Part 1: Minimum Transactions to Settle Multi-Person Debts
Constraints
- 0 <= len(transactions) <= 50
- transactions[i] has exactly 3 integers: [from, to, amount]
- from and to are non-negative integer person IDs
- from != to
- amount > 0
- Let K be the number of people with non-zero final balance. K <= 12
- All intermediate sums fit in a signed 64-bit integer
Examples
Input: ([],)
Expected Output: 0
Explanation: There are no transactions and no one has a non-zero balance.
Input: ([[0, 1, 10]],)
Expected Output: 1
Explanation: One settlement transaction can reverse the imbalance between person 0 and person 1.
Input: ([[0, 1, 10], [2, 0, 5]],)
Expected Output: 2
Explanation: After netting, person 1 has +10 while persons 0 and 2 each have -5, requiring two settlement transactions.
Input: ([[0, 1, 10], [1, 0, 1], [1, 2, 5], [2, 0, 5]],)
Expected Output: 1
Explanation: Only persons 0 and 1 remain imbalanced, so a single settlement transaction is enough.
Input: ([[0, 1, 5], [2, 3, 7]],)
Expected Output: 2
Explanation: There are two independent exact-canceling pairs, so two settlement transactions are optimal.
Hints
- First compress all original transactions into one net balance per person; the original edges no longer matter.
- Use backtracking on the first unsettled balance and try pairing it only with opposite-signed balances. Skipping duplicate balance values and stopping after exact cancellation are useful pruning strategies.
Part 2: Scalable Greedy Debt Settlement Plan
Constraints
- 0 <= len(transactions) <= 200000
- transactions[i] has exactly 3 integers: [from, to, amount]
- from and to are non-negative integer person IDs
- from != to
- amount > 0
- The number of distinct people can be in the hundreds of thousands
- Amounts and final balances fit in a signed 64-bit integer
- You must follow the deterministic greedy rule; exact minimum transaction count is not required
Examples
Input: ([],)
Expected Output: []
Explanation: No balances exist, so the settlement plan is empty.
Input: ([[0, 1, 10]],)
Expected Output: [[1, 0, 10]]
Explanation: Person 1 has positive net balance 10 and pays person 0, who has negative net balance -10.
Input: ([[0, 1, 10], [2, 0, 5]],)
Expected Output: [[1, 0, 5], [1, 2, 5]]
Explanation: Person 1 pays the two receivers with need 5 each. Receiver ID 0 is chosen before receiver ID 2 due to tie-breaking.
Input: ([[0, 1, 7], [1, 0, 7]],)
Expected Output: []
Explanation: The two original transfers cancel out exactly, so no settlement transactions are needed.
Input: ([[0, 2, 5], [1, 3, 5]],)
Expected Output: [[2, 0, 5], [3, 1, 5]]
Explanation: Both payers and receivers are tied by amount, so smaller IDs are matched first.
Hints
- After computing net balances, people with zero balance can be discarded immediately.
- Use two heaps: one for the largest positive balances and one for the largest absolute negative balances. Each greedy match fully settles at least one participant.