Skip to main content

BNPL Transaction Settlement

easy
Software engineer

A financial company provides customers a way to buy something now and pay for it later (BNPL) by issuing them loans. However, the logistics require moving money between multiple parties—customers, merchants, the company, and third-party entities—to ensure everyone is paid correctly upfront.

Given a list of pending transactions and the current end-of-day bank account balances, determine the updated balance for each party after applying all transactions.

A transaction [sender, receiver, amount] indicates that sender pays amount to receiver.

The final output should be a list of integers representing the balances of all unique parties mentioned in either transactions or initialBalance, sorted alphabetically by the party's name.

Example 1

Input

transactions = [["Alice","Bob","50"],["Bob","Charlie","30"],["Charlie","Alice","20"],["Alice","David","70"]], initialBalance = [["Alice","100"],["Bob","50"],["Charlie","75"],["David","25"]]

Output

[0, 70, 85, 95]

Explanation

1. Identify all unique parties: Alice, Bob, Charlie, David.
2. Apply transactions:
   - Alice: Starts with 100. Pays 50 to Bob, pays 70 to David, receives 20 from Charlie. Final: 100 - 50 - 70 + 20 = 0.
   - Bob: Starts with 50. Receives 50 from Alice, pays 30 to Charlie. Final: 50 + 50 - 30 = 70.
   - Charlie: Starts with 75. Receives 30 from Bob, pays 20 to Alice. Final: 75 + 30 - 20 = 85.
   - David: Starts with 25. Receives 70 from Alice. Final: 25 + 70 = 95.
3. Sort parties alphabetically: Alice (0), Bob (70), Charlie (85), David (95).

Example 2

Input

transactions = [["Alice","Bob","300"], ...], initialBalance = [["Alice","1000"], ..., ["Jack","400"]]

Output

[750, 800, 50, 950, 550, 350, 350, 300, 300, 400]

Example 3

Input

transactions = [["Alice","Bob","150"], ...], initialBalance = [["Alice","500"], ...]

Output

[500, 350, -50, 50, 550]

Constraints

  • Initial balances and transaction amounts are integers provided as strings.
  • 1 <= transactions.length <= 10⁴
  • 1 <= initialBalance.length <= 10⁴
  • Party names consist of alphanumeric characters.
  • Amounts are positive integers.

Onsite

ArrayHash TableSimulationSortingString
Language
Code editor loads in the browser.

Output

Input

[["Alice","Bob","50"],["Bob","Charlie","30"],["Charlie","Alice","20"],["Alice","David","70"]]
[["Alice","100"],["Bob","50"],["Charlie","75"],["David","25"]]

Expected

[0,70,85,95]

Your output

Run to see your output.