KatInterview

Funding Ledger with Rail-Specific Settlement and Window Caps

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designsimulationhash-mapheap

Last updated

Simulate an ordered account ledger where each payment rail has its own settlement delay, cutoff-shifted window assignment, and per-window cap, and output the combined results.

Funding Ledger

A fundraising platform lets members receive money into accounts through three payment rails: CARD, ACH and WIRE. Rails differ in how long money takes to become available, which accounting window a deposit is charged to, and how much one account may receive per window.

Write run_funding_ledger(events), which handles a list of events strictly in the given order and returns a single string.

Events

Each event is a list whose first element is its type and whose second element is tick, the current time when the event runs. Ticks never decrease from one event to the next.

  • ["INIT", tick, account_id] creates a new account with nothing accepted and nothing settled.
    • If account_id already exists, the result is "ERROR".
    • Otherwise the event succeeds and produces no output.
  • ["DEPOSIT", tick, account_id, rail, cents] tries to add cents to the account through rail.
    • If the account does not exist, the result is "ERROR".
    • Let W be the window for this tick on this rail (see below). If the amount already accepted for this (account, rail, W) plus cents would be greater than the rail's limit, the result is "REJECTED". A rejected deposit changes nothing at all.
    • Otherwise the result is "ACCEPTED". The amount is added to the (account, rail, W) usage and to the account's accepted total immediately, and it becomes settled at tick tick + settlement_delay.
  • ["QUERY", tick, account_id] reports the account's balances as "{settled}/{accepted}".
    • accepted is the sum of all accepted deposits, whether or not they have settled.
    • settled is the sum of accepted deposits whose settlement tick is less than or equal to the query's tick.
    • If the account does not exist, the result is "ERROR".

Settlement is judged against the tick of the event being processed. A deposit with delay 0 is therefore already settled for any later event at the same tick.

Rail table

Railsettlement_delaycutoffwindow_lengthlimit_cents (per account, per window)
CARD0242420000
ACH317245000
WIRE11224500000

Window assignment

For a rail and a tick, start with tick // window_length. If tick % window_length is greater than or equal to the rail's cutoff, the deposit belongs to the following window, so add 1. Limits are tracked separately for every combination of account, rail and window.

Output

Collect the results of all events that produce output (ERROR, ACCEPTED, REJECTED, and every query result) in event order and join them with commas. If nothing produces output, return an empty string.

Constraints

  • Up to 100,000 events and up to 10,000 distinct accounts.
  • All amounts fit in a 64-bit integer. Use integer arithmetic only.
  • A rejected deposit must not affect window usage, the accepted total, or any pending settlement.
  • Rail names in deposits are always one of CARD, ACH, WIRE.

Examples

Example 1

Input: run_funding_ledger([['INIT', 0, 'alice'], ['DEPOSIT', 1, 'alice', 'CARD', 1000], ['DEPOSIT', 2, 'alice', 'ACH', 2000], ['QUERY', 3, 'alice'], ['QUERY', 5, 'alice'], ['DEPOSIT', 6, 'alice', 'ACH', 4500], ['QUERY', 6, 'alice']])

Output: 'ACCEPTED,ACCEPTED,1000/3000,3000/3000,REJECTED,3000/3000'

Hints

Hint 1

Keep the rail rules in a lookup table keyed by rail name, then compute the window number from the tick with divmod and the cutoff comparison.

Hint 2

Track window usage in a dictionary keyed by (account, rail, window) so that different rails and different windows never share a limit.

Hint 3

Accepted money should be added to the accepted total at once, but to the settled total only later. Put (settle_tick, account, cents) into a min-heap and pop everything due before handling each event.

Hint 4

Remember that a successful INIT adds nothing to the output, and a rejected deposit must leave every counter untouched.