KatInterview

Prepaid Compute Credit Tracker with Replay-Safe Events

Data Structure DesignAuto-graded · 13 test cases
MediumAuto-gradedData Structure Designhash-mapsimulationidempotency

Last updated

Simulate a per-tenant credit ledger driven by a command list, rejecting overdrafts and repeated event ids while tracking balances in a hash map and a set.

Prepaid Compute Credit Tracker

A shared compute cluster hands out prepaid credits to tenants. Each tenant has an integer balance that changes through events. You must simulate a small ledger that is driven by a list of commands.

Event format

An event is a dictionary with these keys:

  • tenantId (string): the tenant whose balance changes.
  • delta (integer): a signed amount. Positive adds credit, negative removes credit.
  • id (string, optional): a unique identifier for the event. If the key is absent, the event has no identifier.

Ledger rules

  • Every tenant starts at balance 0. A tenant never seen before has balance 0.
  • An event is accepted only if both conditions hold:
    1. Its id is absent, or its id has not already been used by a previously accepted event.
    2. The tenant's balance after adding delta would be >= 0.
  • An accepted event adds delta to the tenant's balance and, if it has an id, records that id as used.
  • A rejected event changes nothing. In particular, the id of a rejected event is not recorded, so that id may still be used by a later event.
  • Events without an id are never treated as duplicates.
  • A delta of 0 is a valid event and is accepted if its id is unused.
  • A deduction that brings the balance exactly to 0 is allowed.
  • Ids are tracked globally across all tenants, not per tenant.

Commands

You receive two parallel lists, operations and arguments. The i-th command is operations[i] with argument list arguments[i]. Process the commands in order.

OperationArgumentsBehaviorOutput entry
"init"[events] where events is a list of eventsDiscard all balances and all recorded ids, then apply each event in the given order using the rules above. Events that are rejected are silently skipped.None
"getBalance"[tenantId]Look up the current balance.the integer balance (0 for an unknown tenant)
"applyEvent"[event]Process a single event using the rules above.True if accepted, False if rejected

If no init command has been issued yet, the ledger behaves as if it were empty. Another init later in the list resets everything again.

Task

Return a list with one output entry per command, in the same order as the commands.

Constraints

  • len(operations) == len(arguments)
  • Operation names are exactly "init", "getBalance", or "applyEvent".
  • delta is an integer; balances fit comfortably in Python integers.
  • tenantId values are non-empty strings; id values, when present, are strings.
  • Each command must run in O(1) average time, except init, which is linear in the number of events it contains.

Examples

Example 1

Input: run_credit_ledger(['init', 'getBalance', 'applyEvent', 'getBalance', 'applyEvent', 'getBalance', 'applyEvent', 'getBalance'], [[[{'tenantId': 'gpu-a', 'delta': 10, 'id': 'e1'}, {'tenantId': 'gpu-b', 'delta': 4, 'id': 'e2'}]], ['gpu-a'], [{'tenantId': 'gpu-a', 'delta': -3, 'id': 'e3'}], ['gpu-a'], [{'tenantId': 'gpu-a', 'delta': 5, 'id': 'e3'}], ['gpu-a'], [{'tenantId': 'gpu-a', 'delta': -8, 'id': 'e4'}], ['gpu-a']])

Output: [None, 10, True, 7, False, 7, False, 7]

Hints

Hint 1

Two hash-based structures are enough: a dictionary from tenant to balance and a set of ids that have been used by accepted events.

Hint 2

Write one helper that decides whether an event is accepted and applies it. Use it for both `init` and `applyEvent` so the rules cannot drift apart.

Hint 3

Only record an event's id after you know it was accepted, and make sure `init` replaces both the dictionary and the set with fresh empty ones.