Minimise Transfers to Settle Net Balances Between Linked Accounts
Last updated: November 29, 2026
Quick Overview
Given a list of internal transfers (from_account, to_account, amount) accumulated over a day between marketplace accounts, find the minimum number of transfers needed to settle every account's net balance to zero.
Razorpay
November 29, 20260
0
0 solved
Given a list of internal transfers (from_account, to_account, amount) accumulated over a day between marketplace accounts, find the minimum number of transfers needed to settle every account's net balance to zero.
Netting reduces how many bank transfers RazorpayX must actually execute, and each real transfer costs fees and has failure risk. This hard onsite question tests reduction to net balances, recognising that the answer is n minus the maximum number of zero-sum groups, and a bitmask DP over subsets.
What the Interviewer Expects
- Reduce raw transfers to net balances and drop zeros
- Observe that a zero-sum group of size k needs exactly k - 1 transfers
- Maximise the number of disjoint zero-sum groups via bitmask DP
- State exponential complexity and the practical limit on n
- Contrast with the greedy heuristic used at scale
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- With 10,000 accounts the DP is impossible. What would you ship instead and what does it cost?
- How do you add a constraint that transfers can only happen between accounts at the same bank?
- How would you prove that a greedy largest-creditor-largest-debtor match is within a bounded factor of optimal, or show it is not?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: transfer triples. Output: the minimum count of transfers after netting. Accounts with zero net balance need nothing....
High-Level Design
Net balances first. Key insight: if a set of accounts sums to zero it can be settled with size - 1 transfers (chain them), and no fewer. So with n non-zero accounts partitioned into g zero-sum group...