Practice problem · OpenAI · Medium

GPU Credits

A team runs jobs on GPUs paid for with credits. 2 stages in Python written by you, with an assistant that only answers. Stage 1 free.

Stage 1 of 2: Grants and charges in any order

Background. A team runs jobs on GPUs paid for with credits. Credits come in as grants and go out as charges, and each is recorded with the time it happened. Records reach the ledger in any time order: a grant dated 5 can be recorded after a charge dated 9.

Implement the class CreditLedger in ledger.py with three methods:

  • grant(time, amount) records a grant of amount credits at time.
  • charge(time, amount) records a charge of amount credits at time and returns True if it goes through, False if it is refused.
  • balance_at(time) returns the credits left at time, an integer.

The ledger reads every recorded entry in time order, whatever order they were recorded in. At one time, grants come before charges, and entries of the same kind keep the order they were recorded in. Going through the entries in that order, a grant adds its amount, and a charge goes through if the credits left just before it cover its amount. Otherwise the charge is refused and takes nothing, not even part of its amount.

  • balance_at(time) is what is left after every entry at or before time, and 0 when there is none.
  • charge returns whether the charge goes through in that reading of everything recorded so far, itself included.
  • A refused charge stays in the ledger and is read again with every later answer. A grant recorded later but dated at or before it can let it through. In the same way, a charge recorded later but dated earlier can use credits that a charge dated after it needed, and that charge is then refused. What charge returned stays what was true when it was called.

Example

ledger = CreditLedger()
ledger.grant(10, 50)
ledger.charge(30, 40)   # True: 50 credits at 30
ledger.charge(20, 30)   # True: 50 credits at 20
ledger.balance_at(25)   # 20
ledger.balance_at(30)   # 20: only 20 left at 30, so the charge at 30 is refused
ledger.grant(5, 30)
ledger.balance_at(30)   # 10: with 80 granted, both charges go through

Notes

  • Times are integers from 0 up; amounts are positive integers. Inputs are always valid.
  • balance_at changes nothing.

Done when Submit goes green.

Constraints

  • Python 3.12 standard library only
  • Editable: ledger.py
  • Read-only: all tests/test_*.py files
  • Times are integers from 0 up; amounts are positive integers

What comes after stage 1

The next stage opens once the tests for the one before it pass. Each raises the bar, the way an interviewer does in the room.

  1. Stage 2: Credits that run out. Each grant is usable only for a while. Which credits does a charge use, and what is left at a given time?

The files you get

  • Yours to edit: ledger.py.
  • Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.

The round: no AI, an assistant that only answers

OpenAI runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.

Frequently asked, briefly answered

Is GPU Credits free?

Stage 1 is free with an account. The last stage needs Nyrion Pro.

Which company asks something like this?

OpenAI. It is modelled on OpenAI's coding round, which runs without AI. Based on candidates' reports of OpenAI interviews. Nyrion wrote this version, so the question you get may differ.

Do I bring my own AI tool?

No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.

What does the interviewer ask?

3 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.

Start the problem

Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the first stage of this problem is free.

Start this problem → Sign in

More like it: every practice problem, OpenAI's interviews, the AI coding interview guide.