Skip to content
Interview Prep
All Problems

GPU Credit System

MediumOpenAI Interview
Priority QueueSortingEvent Replay

You're building a system to track balances for an API credit purchasing system.

API users can purchase credit grants, specified by an ID, that are active at a timestamp and that let them use the API. Unused credit grant balances expire at a certain time.

However, there's a kink in our system: requests can arrive out of order because our system is built on a really unstable network. For example, a request to subtract credits can arrive at the system before the request to add credits, even though the request to add credits has a lower timestamp.

Implement the GPUCredit class, which should support the following operations:

Granting credits, subtracting credits, and getting the credit balance for a user
The ability to handle requests that arrive out of order

Guidelines / Hints

Do NOT worry about memory or performance concerns. Write simple, working code.
No fancy data structures are needed here.
All timestamps can be represented as ints for simplicity and are unique (no two actions will happen at the same timestamp).
Subtract from grants expiring soonest first.

Rules

A credit grant is usable for timestamps in [grant_timestamp, grant_timestamp + expiration] (inclusive on both ends).
Balance is never negative. If a use event cannot be fully satisfied, spend as much as possible and ignore the remainder.
Events are replayed in timestamp order regardless of arrival order.
getBalance(t) returns None when the total remaining balance at time t is 0 (no active credits with remaining balance).

Class Skeleton

1class GPUCredit:
2    def __init__(self):
3        pass
4
5    def addCredit(self, creditID: str, amount: int,
6                  timestamp: int, expiration: int) -> None:
7        """
8        Add a credit grant usable during
9        [timestamp, timestamp + expiration] inclusive.
10        """
11        pass
12
13    def useCredit(self, timestamp: int, amount: int) -> None:
14        """
15        Record a credit usage event at the given timestamp.
16        """
17        pass
18
19    def getBalance(self, timestamp: int) -> int | None:
20        """
21        Return total remaining balance at time t,
22        or None if balance is 0.
23        """
24        pass

Examples

Example 1 — Single grant, basic queries

1gpu = GPUCredit()
2gpu.addCredit('microsoft', 10, 10, 30)
3gpu.getBalance(0)   # None  (no active grants)
4gpu.getBalance(10)  # 10
5gpu.getBalance(40)  # 10    (still within [10, 40])
6gpu.getBalance(41)  # None  (expired)

Example 2 — Multiple grants, usage, out-of-order

1gpu = GPUCredit()
2gpu.addCredit('amazon', 40, 10, 50)
3gpu.useCredit(30, 30)
4gpu.getBalance(40)  # 10   (40 - 30 = 10)
5gpu.addCredit('google', 20, 60, 10)
6gpu.getBalance(60)  # 30   (10 from amazon + 20 from google)
7gpu.getBalance(61)  # 20   (amazon expired at 60, google still active)
8gpu.getBalance(70)  # 20   (google active through 70)
9gpu.getBalance(71)  # None (google expired at 70)

Mock Interview Prompt

Copy this prompt into ChatGPT to practice with an AI interviewer.

Act as a senior SWE interviewing me for an entry-level to mid-level engineer position.

I have not done interviews in a long time. Do this step by step:
1. Present the problem below, then ask if I have any clarifying questions before I start. If I say no or try to skip ahead, gently nudge me — say something like "Are you sure? It might be worth thinking about edge cases around ordering or expiration before diving in." Do NOT give specific example questions — just hint at the area.
2. Answer my clarifying questions if I have any, then ask me to walk you through my approach before coding.
3. Once I start coding, give hints only if I appear stuck. Never directly give me the answer — always end with a question that guides me.
4. When I make a suboptimal choice or get something wrong, briefly explain why the alternative is better so I learn the reasoning, then ask me how I'd adjust.
5. Give positive reinforcement when I get something right or show good instincts.

Here is the problem:

You're building a system to track balances for an API credit purchasing system.

API users can purchase credit grants, specified by an ID, that are active at a timestamp and that let them use the API. Unused credit grant balances expire at a certain time.

However, there's a kink in our system: requests can arrive out of order because our system is built on a really unstable network. For example, a request to subtract credits can arrive at the system before the request to add credits, even though the request to add credits has a lower timestamp.

Implement the GPUCredit class with:
- addCredit(creditID, amount, timestamp, expiration)
- useCredit(timestamp, amount)
- getBalance(timestamp) -> int | None

Guidelines:
- Do NOT worry about memory or performance concerns. Write simple, working code.
- All timestamps are ints and are unique.
- Subtract from grants expiring soonest first.
- A credit is usable during [timestamp, timestamp + expiration] inclusive.
- Balance cannot be negative.
- getBalance returns None when balance is 0.
solution.py