PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to convert per-account net balances into a set of transfers, testing algorithmic problem solving, data-structure manipulation, and correctness under conservation invariants in transactional systems.

  • easy
  • Remitly
  • Coding & Algorithms
  • Software Engineer

Compute transfers to balance account debts

Company: Remitly

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

## Problem: Balance accounts by generating transfers You are given a dictionary/map `balances` from `accountId -> netBalance`. - A **positive** balance means the account should **receive** money (is owed money). - A **negative** balance means the account should **pay** money (owes money). - The sum of all balances is guaranteed to be **0**. Produce a list of transfers that will settle all accounts to net 0. ### Output format Return a list of transfers like: - `(fromAccount, toAccount, amount)` where `amount > 0` After applying all transfers: - Every account’s balance becomes exactly `0`. ### Example Input: ```text { A: -5, B: 2, C: 3 } ``` One valid output: ```text [(A, B, 2), (A, C, 3)] ``` ### Notes / constraints - You may return **any** valid set of transfers. - Prefer fewer transfers if possible, but correctness is the priority.

Quick Answer: This question evaluates a candidate's ability to convert per-account net balances into a set of transfers, testing algorithmic problem solving, data-structure manipulation, and correctness under conservation invariants in transactional systems.

You are given a map `balances` from `accountId -> netBalance`. - A **positive** balance means the account should **receive** money (is owed money). - A **negative** balance means the account should **pay** money (owes money). - The sum of all balances is guaranteed to be **0**. Produce a list of transfers `(fromAccount, toAccount, amount)` with `amount > 0` such that, after applying all transfers, every account's balance becomes exactly `0`. **Example** Input: `{A: -5, B: 2, C: 3}` One valid output: `[(A, B, 2), (A, C, 3)]` **Canonical (deterministic) output used by this console** To make the answer reproducible, sort the debtors (accounts that owe money) and the creditors (accounts owed money) by `accountId`, then greedily match the smallest-id debtor against the smallest-id creditor, emitting a transfer of `min(owed, owedTo)` and advancing whichever side reaches `0`. Continue until all balances settle. This yields at most `n - 1` transfers for `n` non-zero accounts. **Notes / constraints** - Any valid set of transfers settles the accounts; this console grades against the canonical sorted-greedy output above. - Accounts with a balance of `0` need no transfer. - An empty map yields an empty transfer list.

Constraints

  • The sum of all balances is exactly 0.
  • Balances are integers; positive = owed money (receive), negative = owes money (pay).
  • 0 <= number of accounts <= 10^5.
  • Accounts with balance 0 are ignored and need no transfer.
  • Return at most n - 1 transfers for n non-zero accounts.

Examples

Input: ({'A': -5, 'B': 2, 'C': 3},)

Expected Output: [('A', 'B', 2), ('A', 'C', 3)]

Explanation: A owes 5; it pays B (2) and C (3) in id order, settling everyone.

Input: ({},)

Expected Output: []

Explanation: No accounts means no transfers.

Input: ({'X': 0},)

Expected Output: []

Explanation: A single account already at 0 needs no transfer.

Input: ({'A': -10, 'B': 10},)

Expected Output: [('A', 'B', 10)]

Explanation: The lone debtor A pays the lone creditor B the full 10.

Input: ({'A': 4, 'B': -6, 'C': 2},)

Expected Output: [('B', 'A', 4), ('B', 'C', 2)]

Explanation: Debtor B owes 6; it pays creditor A (4) then creditor C (2), matched in id order.

Input: ({'A': -3, 'B': -2, 'C': 5},)

Expected Output: [('A', 'C', 3), ('B', 'C', 2)]

Explanation: Two debtors A and B both pay the single creditor C, fully settling C's 5.

Input: ({'D': 7, 'A': -7},)

Expected Output: [('A', 'D', 7)]

Explanation: Debtor A pays creditor D the full 7; sorting puts A's transfer first regardless of input order.

Input: ({'A': -1, 'B': -1, 'C': 1, 'D': 1},)

Expected Output: [('A', 'C', 1), ('B', 'D', 1)]

Explanation: Two independent 1-unit settlements: A->C and B->D, matched smallest-id first.

Hints

  1. Split accounts into debtors (negative balance) and creditors (positive balance); the absolute owed amount equals the magnitude of the balance.
  2. Sort both groups by accountId so the matching order is deterministic and reproducible.
  3. Use two pointers: transfer min(debtorOwed, creditorOwed) from the current debtor to the current creditor, subtract it from both, and advance whichever side hits 0.
  4. Stop when either group is exhausted; because the total sums to 0, both finish together.
Last updated: Jun 26, 2026

Related Coding Questions

  • Solve k-Nearest Places by Latitude/Longitude - Remitly (Medium)
  • Design an Elevator System and Scheduler - Remitly (Medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.