Problem · Design

GPU Credit Ledger

Learn this problem
HardOpenAI logoOpenAIFULLTIMEPHONE SCREEN

Problem statement

Implement a time-aware GPU credit ledger. Grants add credits for a bounded time interval, spending events consume active credits, and balance queries ask how many credits remain at a timestamp.

Supported operations

  • ADD grantId amount start expire: register a grant of amount credits. The grant is active for timestamps start <= t < expire.
  • SUBTRACT amount timestamp: consume amount credits active at timestamp. Consume credits from the grants that expire earliest. The consumption continues to affect later balances until those grants expire. Grants that begin after the subtraction timestamp cannot pay for it. If the active balance is insufficient, do not consume any credits.
  • BALANCE timestamp: append the remaining active balance at timestamp to the output.

Operations are registered in input order, but their timestamps do not have to be sorted. For events at the same timestamp, expiring grants are no longer active, new grants are active, and then subtraction events are applied in input order.

Function

processGpuCredits(operations: String[]) → String[]

Examples

Example 1

operations = ["ADD a 4 20 60", "ADD b 3 30 40", "SUBTRACT 2 30", "BALANCE 30", "BALANCE 40"]return = ["5", "4"]

At timestamp 30, both grants are active. The subtraction consumes two credits from grant b because it expires first, leaving a balance of five. At timestamp 40, grant b has expired and grant a still has all four credits.

More OpenAI problems

drafts saved locally
public String[] processGpuCredits(String[] operations) {
  // write your code here
}
operations["ADD a 4 20 60", "ADD b 3 30 40", "SUBTRACT 2 30", "BALANCE 30", "BALANCE 40"]
expected["5", "4"]
checking account