KatInterview

Expiring Compute Credits: Offline Ledger Replay

Heaps, Sorting & SearchAuto-graded · 18 test cases
MediumAuto-gradedHeaps, Sorting & Searchheapsortingsimulation

Last updated

Replay a shuffled log of credit grants, spends and balance queries in time order, spending the soonest-expiring credits first, using a heap and sorting.

You are given the complete log of operations performed on a compute-credit ledger. The log is not in chronological order, but each operation carries its own timestamp. Compute the answer to every operation as if all operations had been applied in timestamp order.

Operations

Each operation is a list in one of these forms:

  • ["add_credit", id, amount, timestamp, expiration] grants amount credits that can be used during the half-open interval [timestamp, timestamp + expiration). The credits are usable at timestamp itself but no longer usable at timestamp + expiration. The id is only a label, and two grants with the same id are independent.
  • ["charge", amount, timestamp] tries to spend amount credits at timestamp.
  • ["get_balance", timestamp] asks for the total number of usable (granted, started, not yet expired, not yet spent) credits at timestamp.

Rules

  1. Chronological replay. Operations take effect in increasing order of timestamp, regardless of their position in the input list.
  2. Ties at the same timestamp. All add_credit operations happen first, then all charge operations, then all get_balance operations. Within the same kind, operations are handled in input order.
  3. Usable credits. A charge at time t can only use grants with timestamp <= t < timestamp + expiration that still have credits left. A grant with amount <= 0 or expiration <= 0 is ignored entirely.
  4. Partial charges. A charge spends min(amount, current balance) credits. It never fails. It takes as much as is available, up to the requested amount.
  5. Spending order. A charge takes credits from the grant that expires soonest first, then the next soonest, and so on. If two grants expire at the same moment, which one is drained first does not affect any output.

Output

Return a list with one entry per input operation, in the original input order:

  • None for add_credit
  • the number of credits actually spent for charge
  • the balance for get_balance

Constraints

  • 1 <= len(operations) <= 10^5
  • Timestamps and expirations are integers; timestamps may be repeated and may appear in any order.
  • Charge amounts are positive integers. Grant amounts are integers (non-positive ones are ignored).
  • The expected running time is O(n log n).

Examples

Example 1

Input: replay_credit_ledger([['add_credit', 'gpu-a', 10, 10, 10], ['add_credit', 'gpu-b', 5, 0, 100], ['charge', 8, 12], ['get_balance', 12], ['get_balance', 20]])

Output: [None, None, 8, 7, 5]

Example 2

Input: replay_credit_ledger([['add_credit', 'x', 7, 5, 5], ['get_balance', 5], ['charge', 10, 7], ['get_balance', 7], ['get_balance', 10]])

Output: [None, 7, 7, 0, 0]

Hints

Hint 1

Because the whole log is known in advance, you do not need an online structure. Sort all operations by (timestamp, kind, input index), with kind order add < charge < query, and sweep once.

Hint 2

Keep a min-heap of grants keyed by expiry time. Before handling each event at time t, pop every grant whose expiry is <= t and subtract its remaining amount from a running balance.

Hint 3

A charge spends min(amount, balance). To spend it, repeatedly take the heap top (soonest expiry), use as much as needed, and push it back with the leftover if any. Store the answer at the operation's original index.