KatInterview

Ledger Replay Cleanup: Apply Each Payment Event Once

Arrays & HashingAuto-graded · 19 test cases
EasyAuto-gradedArrays & Hashinghash-sethash-mapdeduplication

Last updated

Process a stream of possibly redelivered payment events and compute final account balances by applying each unique event ID only once.

Ledger Replay Cleanup

A payments service receives events from a message broker that guarantees every event arrives at least once. Occasionally a worker crashes before confirming an event, so the same event is delivered again. Your job is to rebuild account balances from the stream so that no event is counted more than once.

Task

Write a function that takes the starting balances and the list of received events, and returns the final balance of every account.

Input

  • starting_balances: a dictionary mapping an account name (string) to its integer starting balance.
  • events: a list of events, in the order they were received. Each event is a list of three items: [event_id, account, amount].
    • event_id is a string identifying the event.
    • account is a string naming the account to change.
    • amount is an integer to add to that account's balance. A negative amount is a debit and zero is allowed.

Rules

  1. Process events in the given order.
  2. The first time an event_id appears, add its amount to the balance of its account.
  3. If an event_id has already been seen, ignore the event completely. This holds even if the repeat names a different account or amount. The first occurrence always wins, and uniqueness of IDs is global across all accounts.
  4. An account that is not in starting_balances starts at 0 when it first receives an applied event.
  5. Accounts in starting_balances must appear in the result even if no event touches them.
  6. Do not modify the input dictionary. Return a new dictionary.
  7. If events is empty, return a copy of starting_balances.

Output

A dictionary mapping each account name to its final integer balance.

Constraints

  • Account names and event IDs are strings.
  • Amounts and balances are integers (positive, negative, or zero).
  • The number of events can be large, so aim for a solution that runs in linear time in the number of events.
  • Each event list has exactly three elements.

Examples

Example 1

Input: rebuild_balances_once({}, [['tx1', 'alice', 100], ['tx1', 'alice', 100], ['tx2', 'alice', -30]])

Output: {'alice': 70}

Example 2

Input: rebuild_balances_once({'alice': 50, 'bob': 20}, [['a', 'alice', 10], ['b', 'bob', -5], ['a', 'alice', 10], ['c', 'alice', 7]])

Output: {'alice': 67, 'bob': 15}

Example 3

Input: rebuild_balances_once({'alice': 0}, [['t1', 'alice', 100], ['t2', 'alice', 25], ['t1', 'alice', 100], ['t2', 'alice', 25]])

Output: {'alice': 125}

Hints

Hint 1

You need a fast way to ask whether an event ID has already been handled. Which built-in structure gives average O(1) membership checks?

Hint 2

Start from a copy of the starting balances so the caller's dictionary is untouched, and use a default of 0 for accounts you have not seen before.

Hint 3

Mark the event ID as seen only when you apply it, and skip any later event with an ID already in your seen collection, regardless of its account or amount.