Compute Balances and Minimize Settlements
Company: Affirm
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates transactional data aggregation and net-balance computation skills alongside combinatorial optimization for minimizing settlement transfers, testing competencies in data structures, numeric aggregation, and algorithmic reasoning.
End-of-Day Net Balances
Constraints
- delta_amount is an integer and may be negative, positive, or zero.
- A user may appear in many records; sum all their deltas.
- Omit any user whose final net balance equals 0.
- The input list may be empty (return an empty mapping).
Examples
Input: ([[1, 'alice', 100], [2, 'bob', -50], [3, 'alice', -30], [4, 'carol', 20]],)
Expected Output: {'alice': 70, 'bob': -50, 'carol': 20}
Explanation: alice nets 100-30=70, bob -50, carol 20. All non-zero, all kept.
Input: ([],)
Expected Output: {}
Explanation: No transactions means no balances.
Input: ([[1, 'alice', 50], [2, 'alice', -50]],)
Expected Output: {}
Explanation: alice nets to 0, so she is omitted, leaving an empty result.
Input: ([[1, 'x', -10], [2, 'y', 10]],)
Expected Output: {'x': -10, 'y': 10}
Explanation: x owes 10 (debtor), y is owed 10 (creditor); total balance is 0.
Input: ([[1, 'a', 5], [1, 'a', 5], [2, 'a', -3], [3, 'b', -7]],)
Expected Output: {'a': 7, 'b': -7}
Explanation: Multiple records for 'a' (including a same-timestamp duplicate) sum to 5+5-3=7; 'b' is -7.
Hints
- A single hash map keyed by user_id is enough — accumulate delta_amount per user in one pass.
- timestamp is not needed for the net balance; you only sum the deltas.
- After accumulating, filter out users whose balance is exactly 0 so settled users don't appear.
Minimum-Transfer Settlement Plan
Constraints
- The number of users with non-zero balance is small (an exponential-time solution is acceptable).
- The sum of all balances is exactly 0.
- Users with a balance of 0 are ignored and do not appear in any transfer.
- Greedy largest-creditor/largest-debtor pairing is NOT guaranteed optimal; subsets that cancel exactly must be found.
Examples
Input: ({},)
Expected Output: 0
Explanation: No non-zero balances means no transfers are needed.
Input: ({'a': 10, 'b': -10},)
Expected Output: 1
Explanation: b pays a 10 in a single transfer.
Input: ({'a': 5, 'b': 5, 'c': -10},)
Expected Output: 2
Explanation: c must pay both a and b; no zero-sum proper subgroup exists, so 3 accounts settle in 3-1=2 transfers.
Input: ({'a': 4, 'b': -1, 'c': -3},)
Expected Output: 2
Explanation: The classic case: {4,-1,-3} has no two-element zero-sum subset, so it settles in 3-1=2 transfers, not 1.
Input: ({'a': 1, 'b': 1, 'c': -2, 'd': 3, 'e': -3},)
Expected Output: 3
Explanation: Partition into {a,b,c} (sums to 0, 2 transfers) and {d,e} (1 transfer) = 2 groups; 5-2=3.
Input: ({'a': -100, 'b': 50, 'c': 50},)
Expected Output: 2
Explanation: a pays both b and c; no zero-sum subgroup, so 3-1=2 transfers.
Input: ({'w': 5, 'x': -5, 'y': 6, 'z': -6},)
Expected Output: 2
Explanation: Two independent zero-sum pairs {w,x} and {y,z}; 2 groups, 4-2=2 transfers.
Input: ({'a': 3, 'b': 2, 'c': -5, 'd': 4, 'e': -4, 'f': 1, 'g': -1},)
Expected Output: 4
Explanation: Three zero-sum groups {a,b,c}, {d,e}, {f,g}; 7-3=4 transfers.
Hints
- The minimum number of transfers for a set whose total is 0 and that cannot be split into smaller zero-sum subsets is (count - 1).
- So minimize total transfers by MAXIMIZING the number of disjoint subsets that each sum to exactly 0; the answer is n - (max number of such subsets).
- Use bitmask DP over the non-zero accounts: precompute each subset's sum, then for each zero-sum mask, anchor on its lowest set bit and try every zero-sum sub-group containing that bit.