KatInterview

Expiring Resource Credit Ledger with Out-of-Order Events

Data Structure DesignModel answer
HardModel answerData Structure Designdesign-dsheaps-sortinggreedy-intervals

Last updated

Design an in-memory compute credit ledger handling expiring grants, usage subtractions with earliest-expiration priority, out-of-order event ingestion, and point-in-time balance queries.

Design an in-memory credit ledger system for allocating and consuming compute capacity (e.g., GPU execution time).

Credits are issued via grants that are active during a specific time window. Usage is recorded via subtractions. Callers can query the total unexpired and unconsumed balance at any arbitrary historical or present timestamp $t$.

Events may arrive out of chronological order.

System Operations

  1. add_credit(grant_id, amount, timestamp, expiration) Registers a credit grant of amount units. The grant becomes active at timestamp and expires at expiration. It is usable in the half-open interval $[timestamp, expiration)$.
  2. subtract(amount, timestamp) Registers credit consumption of amount units at timestamp.
  3. get_balance(timestamp) Calculates and returns the total usable, non-expired credits remaining at timestamp, accounting for all events registered with an event timestamp $\le timestamp$.

Rules & Determinism

  • Lazy Evaluation: add_credit and subtract merely record events. Ledger balance evaluation happens when queries are issued.
  • Tie-Breaking: If an addition and a subtraction share the same timestamp, process credit additions before subtractions. If multiple subtractions share a timestamp, apply them in the order they were recorded.
  • Earliest Expiration First (EEF): When consuming credits at timestamp $t$, draw from active grants that have the earliest expiration time. If the grant expiring earliest cannot cover the requested amount, consume it entirely and draw the remainder from the grant with the next earliest expiration time.
  • Overdraft Limit: If a subtraction exceeds total available active credits at timestamp $t$, exhaust all active credits (reducing balance to 0) and ignore the unsatisfied portion. Subtractions never result in a negative balance or create debt against future grants.
  • Retroactive Mutation: An out-of-order event arriving at timestamp $t_{late}$ logically updates state for all time steps $t \ge t_{late}$.

Implementation Requirements

Part 1: Baseline Replay

Implement the ledger API assuming replaying events from time zero on every get_balance query is acceptable.

Part 2: Optimized Point-in-Time Ledger

Replaying from time zero per query scales poorly ($O(N \log N)$ per query). Design an optimized approach using snapshot checkpointing and selective invalidation.

Explain:

  1. The checkpointing data structure and memory footprint.
  2. How out-of-order event insertion invalidates cached checkpoints.
  3. The role and runtime complexity of priority queues and binary search in the query path.

Hints

Hint 1

Sort events primarily by timestamp. At equal timestamps, process credit additions before subtractions.

Hint 2

Use a min-heap ordered by grant expiration time to greedily select which credits to deduct first.

Hint 3

For Part 2, store chronological state checkpoints. When an event arrives with timestamp t, drop or invalidate all checkpoints with timestamp >= t.