Fewest Transfers to Settle Shared Expenses Among Friends

Last updated: November 29, 2026

Quick Overview

A group of friends records who paid whom in a shared-expenses app. Return the minimum number of transfers needed so everyone's net balance becomes zero.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A group of friends records who paid whom in a shared-expenses app. Return the minimum number of transfers needed so everyone's net balance becomes zero.

A Google onsite problem in the spirit of splitting costs in Google Pay groups. The interviewer checks that you reduce transactions to net balances, drop zeros, and backtrack by settling the first non-zero person against each opposite-signed person.

What the Interviewer Expects
  • Reduces raw transactions to net balances and drops zeros
  • Backtracks by always settling the first unresolved balance
  • Only pairs it with balances of the opposite sign
  • Skips duplicate balance values at the same level
  • Mentions the subset-sum view: answer = n minus the max number of zero-sum groups
Key Topics to Cover
Backtracking
Net balances
Pruning
Subset sum
Bitmask DP
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Implement the bitmask DP that maximizes the number of zero-sum groups.
  • Why can the greedy biggest-debtor-pays-biggest-creditor heuristic be suboptimal?
  • How would you present the actual transfers in the app?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: list of [from, to, amount] transactions (up to about 8 to 12 people, amounts positive integers).
  • Output: minimum number of transfers to settle all debts.
  • Example: [[0,1,10],[2,0,5]] g...
High-Level Design
  1. Compute net balance per person: payer +amount, receiver -amount. Discard zeros. Who paid whom no longer matters, only balances.
  2. DFS over the balance list. Skip leading zeros to find the first un...

Submit Your Answer
Markdown supported