GPU Credit Ledger
Asked at OpenAI
Reported by the problem's creator, not verified
Build an in-memory ledger of expiring GPU credits for many users. Each grant adds a separate lot of credits that stops being usable at its expiry time; a charge is always taken from the lots that expire soonest. Users never affect each other.
A lot is valid at time t exactly when t < expiry. At t == expiry it is already expired: it does not count toward the balance and cannot be spent.
Every method that takes a time at is called with times that never decrease from one such call to the next, across all users. grant takes no time and can be called at any point.
Each test drives one CreditLedger() instance through a sequence of calls and checks every return value.
Examples
Example 1
Input: init =
[], operations =[["grant",["alice",10,5]],["grant",["alice",30,20]],["charge",["alice",10,5]],["balance",["alice",5]],["charge",["alice",21,6]],["balance",["alice",6]]]Output:
[null,null,true,20,false,20]Explanation: The lot of 10 expires at 5, so at time 5 it is already gone: the charge of 10 at time 5 is taken from the 30-lot, leaving 20. The charge of 21 at time 6 needs more than the 20 left, so it fails and changes nothing.