As reported: OpenAI coding round (October 2025), after the phone screen. The round provided test cases and the function signatures.
Implement a credit ledger with three calls:
transaction(timestamp, id, amount, expirationTimestamp)— grantamountcredits (identified byid) that are usable fromtimestampuntilexpirationTimestamp. Whatever is left of them atexpirationTimestampis voided.subtract(timestamp, amount)— spendamountcredits attimestamp.getBalance(timestamp)— the number of usable credits attimestamp.
Calls do not arrive in timestamp order: a transaction stamped earlier may be reported after later calls, and every answer must take all calls received so far into account.
Clarifications
- A grant is usable at time
twhentimestamp ≤ t < expirationTimestamp. getBalance(t)is the state after applying, in timestamp order, everytransactionandsubtractreceived so far whose timestamp is≤ t. At equal timestamps, transactions apply before subtracts; otherwise calls keep the order they were received in.- A
subtractspends from the usable grant that expires soonest first, moving on to the next one as each runs out. - A
subtractlarger than the usable balance at its timestamp is rejected and spends nothing.
Example
transaction(10, "a", 50, 100)
transaction(5, "b", 30, 20) // reported late
getBalance(15) -> 80
subtract(12, 40) // b expires first: 30 from b, 10 from a
getBalance(15) -> 40
getBalance(7) -> 30 // only b exists at 7; the subtract happens at 12
getBalance(100) -> 0 // a has expired
subtract(30, 100) // only 40 usable at 30: rejected
getBalance(50) -> 40
Constraints: at most 1000 calls; 1 ≤ timestamp < expirationTimestamp ≤ 10^9; 1 ≤ amount ≤ 10^6; ids are 1–10 letters or digits and unique.
Code runner format: the first line is the number of calls q; each of the next q lines is transaction ts id amount exp, subtract ts amount or getBalance ts. The harness prints the result of every getBalance on its own line.