KatInterview

Credit System with Expiration and Time-Ordered Replay

Data Structure DesignModel answer
MediumModel answerData Structure Designheaptime-orderingdata-structure-design

Last updated

Build a credit tracking system where allocations expire, spending prioritizes earliest expiration, and operations arrive out of chronological order.

You're building the accounting layer for a cloud resource platform. User accounts hold credits that come from allocations and get consumed through usage. Each allocation has a lifetime, and unused portions vanish when that lifetime ends.

Your system must support three operations:

  • grant(grant_id, amount, timestamp, expiration) records an allocation of amount credits, valid from timestamp through expiration (exclusive).
  • subtract(amount, timestamp) consumes amount credits at time timestamp.
  • get_balance(timestamp) computes how many credits are available at time timestamp.

Two constraints increase the complexity:

  1. Spending order by expiration. When credits are consumed, they must come from the valid allocation expiring soonest, then the next soonest, continuing until the full amount is covered.
  2. Non-chronological arrival. Operations arrive in arbitrary order. An operation with timestamp t₁ might arrive after one with timestamp t₂ > t₁, yet all balance queries must remain consistent with the actual chronological sequence.

Work with a single account (multiple accounts are independent instances), positive integers for amounts, and integer timestamps.

Constraints

  • Each allocation satisfies expiration > timestamp, and every grant_id is unique.
  • get_balance(t) must account for every operation received so far with timestamp ≤ t, regardless of arrival order.

Clarifying Questions

Before implementing, confirm:

  • Is an allocation valid at exactly its expiration timestamp, or does it become invalid then?
  • If a consumption request exceeds available credits at its timestamp, what happens: rejection, partial application, or negative balance?
  • When an allocation and a consumption share the same timestamp, which is processed first?
  • When multiple valid allocations expire simultaneously, how is the tie broken?
  • Is there a bound on how far back in time an operation can arrive?

Part 1: Core Implementation

Implement the three operations with correct handling of expiration priority and out-of-order arrivals. State time and space complexity for each operation.

Your solution should address:

  • Precise semantics for boundary conditions, tie-breaking, and overdraft scenarios
  • A mechanism to identify the earliest-expiring valid allocation at any timestamp
  • Correct balances when a late-arriving operation predates already-processed operations
  • Complexity analysis

Part 2: Production Optimization

A naive implementation might recompute from the earliest timestamp on every balance query. Discuss how you would optimize this for production workloads where replaying the entire history is too expensive.

Additional context to consider:

  • Is there a cutoff after which late operations are rejected or handled as adjustments?
  • Are balance queries typically for the current moment, or for arbitrary historical timestamps?

Your answer should cover:

  • Maintaining intermediate state and handling invalidation when late operations arrive
  • Compacting settled history so memory and computation don't grow unbounded
  • Query cost and late-operation cost under the optimized approach

Hints

Hint 1

Store operations in timestamp order, not arrival order, to replay the correct sequence

Hint 2

Use a priority queue to efficiently find which allocation expires next among those currently valid

Hint 3

Consider checkpointing balances at certain timestamps to avoid full replay from the beginning