22A GPU credit ledger with expiring credits
22 A GPU credit ledger with expiring credits
OpenAI's second most reported coding question is a GPU credit ledger, from phone screens and onsites (7 reports). The shape: grant credits that become usable at one time and expire at another, spend them, and report the balance at any timestamp (3 reports). Spends use the credits that expire soonest first (2 reports). Calls don't arrive in timestamp order, so the ledger has to replay history correctly (2 reports). In the version candidates call GPU Credit II, once the balance would go negative, every later balance is None (1 report). One candidate called the whole thing 30 to 40 lines (1 report); another fought edge cases until time ran out and needed hints to finish (1 report). This is practice in that shape, not a leaked question.
Set a 60-minute timer. Read one level at a time, and run only that level's tests until they pass:
pytest problems/22_gpu_credits -k level1. Earlier levels must keep passing.
Build CreditLedger in solution.py. Timestamps and amounts are ints. Calls can arrive in any timestamp order: a
grant or spend recorded now can land before a balance you already read, and balance must reflect it.
Level 1. grant(credit_id, amount, start) adds amount credits that count from start on.
spend(amount, timestamp) uses credits. balance(timestamp) returns what's left at that time: every grant with
start <= timestamp, minus every spend with a timestamp <= timestamp. A grant with a repeated credit_id, or a
grant or spend whose amount isn't positive, raises ValueError and changes nothing. Level 1 tests never spend more than there is.
Level 2. Credits expire. grant(credit_id, amount, start, expires=None): a credit can be used while
start <= t < expires, and whatever is left of it at expires is gone. expires=None never expires, and
expires <= start raises ValueError and stores nothing, so the same credit_id can be granted again. A spend draws from the usable credit that expires soonest, and credit that
never expires goes last. Now the order matters: spend from the wrong grant and credit that should have been used
expires instead. Level 2 tests never spend more than is usable.
Level 3. Overdraft. A spend that needs more than the usable credit at its timestamp overdraws the ledger, and
balance returns None at that timestamp and every later one. Balances before it don't change. Because calls
arrive out of order, a grant recorded later can cover the spend and clear the overdraft, and a spend recorded later
can cause one.
Things to get right: ties at one timestamp (a grant that starts at t can pay for a spend at t, and a credit that
expires at t can't), replaying in timestamp order whatever order the calls came in, and never-expiring credit
last. Follow-ups from the reports: how to avoid replaying from time zero on every balance (1 report), and what
changes with a huge number of credit changes (1 report). Be ready to say how you'd show which grants paid for a
spend, too.
Graded the way OpenAI says it grades: design, code quality, performance, tests (1 source).