KatInterview

Expiring Compute Credits: Earliest-Expiry-First Ledger Simulation

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designheapsimulationgreedy

Last updated

Simulate a credit ledger replaying a command list, spending the soonest-expiring grants first, with all-or-nothing spends and time-based expiry.

Expiring Credit Ledger

A compute platform hands out credits in separate grants. Each grant has a size and an expiry time. When a job runs, its cost is paid from whichever usable grants will expire soonest, so as little credit as possible goes to waste.

You are given a recorded sequence of ledger commands and must replay it, returning one result per command.

Commands

commands[i] is the name of the i-th command and params[i] is its list of arguments.

CommandArgumentsMeaningResult appended
"add_grant"[grant_id, amount, expire_at]Register a grant of amount credits.None
"consume"[timestamp, amount]Try to spend amount credits at time timestamp.True or False
"get_balance"[timestamp]Report the total usable credits at time timestamp.an integer

Rules

  • Validity. A grant is usable at time t only if t < expire_at. At t == expire_at it is already expired.
  • Clock. The ledger's "current time" starts at 0. Every consume and get_balance command sets the current time to its timestamp. add_grant has no timestamp and does not change the clock.
  • Late grants. If an add_grant arrives with expire_at <= the current time, the grant is already expired and has no effect. It still produces a None result. Grants added with expire_at greater than the current time are valid from the moment they are added.
  • Spending order. consume draws from usable grants in increasing expire_at order. A grant is drained completely before the next one is touched. Grants with equal expire_at expire together, so the order among them cannot change any result.
  • All or nothing. If the total usable credit at timestamp is less than amount, return False and change nothing. Otherwise deduct exactly amount and return True. Spending exactly the full balance succeeds.
  • Balance. get_balance returns the sum of remaining credits over all grants usable at timestamp, or 0 if there are none.
  • Time order. Across all consume and get_balance commands, timestamps never decrease.

Return the list of results, one per command and in the same order.

Constraints

  • 1 <= len(commands) == len(params) <= 2 * 10^5
  • commands[i] is one of "add_grant", "consume", "get_balance".
  • 1 <= amount <= 10^9
  • 1 <= timestamp, expire_at <= 10^14
  • grant_id is a unique, non-empty alphanumeric string. It does not affect any result.
  • Timestamps in consume and get_balance commands are non-decreasing in command order.

Examples

Example 1

Input: process_ledger_ops(['add_grant', 'add_grant', 'consume', 'get_balance', 'consume', 'get_balance'], [['g1', 100, 50], ['g2', 50, 30], [10, 80], [20], [40, 70], [45]])

Output: [None, None, True, 70, True, 0]

Example 2

Input: process_ledger_ops(['add_grant', 'get_balance', 'consume'], [['g1', 50, 20], [20], [20, 10]])

Output: [None, 0, False]

Hints

Hint 1

Spending order and expiry order are the same order: always the smallest expire_at first. One min-heap keyed by expire_at can serve both purposes.

Hint 2

Because query times never go backward, a grant that has expired can never become usable again. Pop expired grants off the heap permanently before handling each consume or get_balance, and keep a running total of the credits still in the heap.

Hint 3

For consume, compare the running total with the amount before changing anything, so a failure leaves no trace. On success, repeatedly take from the heap top: pop it if fully used, otherwise reduce its remaining amount in place. Reducing it does not change its heap key.