KatInterview

Compute Credit Ledger with Expiration

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designheappriority-queuesimulation

Last updated

Implement an in-memory ledger that manages time-limited credits with priority expiration and debt tracking.

You are building a ledger to manage time-limited compute credits. Users receive credit allocations that become invalid after a certain time, and when they use resources, the system must prioritize spending credits that will expire soonest to maximize value.

Your task is to implement a function that processes a sequence of ledger operations:

  • grant: Add a credit allocation of a given amount, valid from a start time until an expiration time (exclusive).
  • consume: Use credits at a given time, returning any outstanding debt after the operation.
  • get_balance: Query the net balance (active credits minus any debt) at a given time.

Input

  • operations: A list of operation names, each one of "grant", "consume", or "get_balance".
  • arguments: A list of argument lists, one per operation:
    • grant: [amount, timestamp, expiry] — adds amount credits valid from timestamp until expiry (exclusive)
    • consume: [amount, timestamp] — uses amount credits at timestamp
    • get_balance: [timestamp] — queries the balance at timestamp

Output

Return a list of results, one per operation:

  • grant → None
  • consume → integer representing outstanding debt after consumption
  • get_balance → integer representing net balance (positive for surplus, negative for debt)

Behavior Rules

  1. Earliest-expiry consumption: When consuming, always use credits with the earliest expiration first.
  2. Expiration boundary: A credit batch with expiration time e cannot be used at any time t >= e.
  3. Debt accumulation: If a consume request exceeds available active credits, consume all active credits and record the shortfall as debt (negative balance).
  4. Debt settlement: When granting new credits while debt exists, the new credits immediately pay down the debt first. Only any surplus remains as active credits with the given expiration.
  5. Monotonic time: All timestamps are non-decreasing across operations.
  6. Expired grants: A grant that has already expired when issued (where expiry <= timestamp) has no effect.

Constraints

  • 1 <= len(operations) <= 2 * 10^5
  • 0 <= amount <= 10^9
  • 0 <= timestamp < expiry <= 10^9
  • timestamp is non-decreasing across all operations

Examples

Example 1

Input: compute_credit_ledger(['grant', 'grant', 'consume', 'get_balance', 'consume', 'get_balance', 'grant', 'get_balance'], [[100, 0, 10], [50, 2, 5], [70, 3], [4], [90, 8], [8], [30, 12, 20], [12]])

Output: [None, None, 0, 80, 10, -10, None, 20]

Example 2

Input: compute_credit_ledger(['grant', 'get_balance', 'consume', 'get_balance'], [[50, 0, 5], [5], [20, 6], [6]])

Output: [None, 0, 20, -20]

Hints

Hint 1

Use a min-heap keyed by expiration time to always access the soonest-expiring credits first.

Hint 2

Since time only moves forward, expired credits can be removed lazily at the start of each operation.

Hint 3

Track two values outside the heap: total active credits and accumulated debt. Either debt is zero or the heap is empty, never both.