Fewest Transfers to Settle Shared Expenses Between Members

Last updated: November 29, 2026

Quick Overview

A group of members split expenses, recorded as (payer, beneficiary, cents) entries. Compute the minimum number of direct transfers needed so that everyone ends up even.

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

0

0

0 solved


A group of members split expenses, recorded as (payer, beneficiary, cents) entries. Compute the minimum number of direct transfers needed so that everyone ends up even.

Peer to peer payments in SoFi's money app make split bills a realistic feature, and minimizing transfer count reduces fees and notification noise. The interviewer checks that you reduce the problem to net balances, recognize it is NP-hard in general, and write a pruned backtracking search with a correctness argument.

What the Interviewer Expects
  • Reduces the ledger to each person's net balance and drops zeros
  • Explains that a naive greedy (largest creditor with largest debtor) is not always optimal
  • Backtracks by settling the first non-zero balance against each opposite-sign balance
  • Prunes with a best-so-far bound and skips duplicate values
  • Mentions the bitmask DP alternative based on zero-sum subsets
Key Topics to Cover
backtracking
pruning
net balances
NP-hard search
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
  • What is the answer in terms of the maximum number of disjoint zero-sum groups?
  • How would you handle 30 people where backtracking gets slow?
  • How would you constrain transfers to people who are already connected 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

Each entry means the payer covered the beneficiary, so the beneficiary owes the payer. Output the minimum number of transfers. Groups are small (up to about 12 people with non-zero net), which justifi...

High-Level Design

Only net balances matter. If n people have non-zero net, n - 1 transfers always suffice, and the true minimum is n minus the maximum number of disjoint subsets that each sum to zero. Greedy matching o...


Submit Your Answer
Markdown supported

Related Questions