Find the minimum number of payments to settle debts between users

Last updated: August 6, 2025

Quick Overview

Given a list of transactions between users (who paid whom and how much), find the minimum number of payments needed to settle all debts. This models Affirm's internal settlement optimization between merchants.

Affirm
Coding & Algorithms
Software Engineer
Affirm
August 6, 2025
Software Engineer
Technical Phone Screen
Coding & Algorithms
Hard

14

4

3,711 solved


Given a list of transactions between users (who paid whom and how much), find the minimum number of payments needed to settle all debts. This models Affirm's internal settlement optimization between merchants.

This algorithmic problem appears in Affirm's technical phone screens. It combines graph theory with financial concepts and tests your ability to optimize real-world financial operations.

What the Interviewer Expects
  • Reduce the problem to net balances first (simplify the transaction graph)
  • Recognize this as a subset-sum partitioning problem for optimal solution
  • Implement a greedy approximation that works well in practice
  • Discuss time complexity and why optimal solution is NP-hard
  • Handle edge cases: circular debts, zero balances, single user
Key Topics to Cover
Graph algorithms and simplification
Greedy algorithms
NP-hard problem recognition
Financial settlement optimization
Backtracking and pruning
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
  • How would you handle the case where some payment channels have fees?
  • Can you prove that your greedy solution is not always optimal?
  • How would you extend this to minimize total amount transferred rather than number of transfers?
  • How would you handle currency conversion in multi-currency settlements?
Sharpen Your Skills on Codemia

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

Practice DSA Problems
Sample Answer
Problem Analysis

To solve the problem of minimizing the number of payments needed to settle debts, we first need to recognize that this can be modeled as a graph where users are nodes and transactions are directed edg...

Approach
  1. Calculate Net Balances: Start with an array of balances initialized to 0 for each user. For each transaction (payer, payee, amount), decrement the payer's balance and increment the payee's bala...

Submit Your Answer
Markdown supported

Related Questions