Design a GPU credit system with overlapping expiring grants and out-of-order events

Quick Overview

Design a GPU credit system that adds overlapping grants with time windows, consumes credits from the soonest-expiring grant first, and answers balance queries at any timestamp. It tests all-or-nothing updates, greedy ordering, idempotent adds and handling calls that arrive out of timestamp order.

Design a GPU credit system with overlapping expiring grants and out-of-order events

Company: OpenAI

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

You are designing a system to manage GPU credits. Each credit grant has a unique `id` and is valid during a time window from `start` to `end`, inclusive. Credit grants may overlap in time. Because of network delays, calls such as adding or consuming credits may arrive and be processed out of order with respect to their timestamps. Each call is applied to the current state when it arrives; its timestamp only decides which grants are active for that call. At any point you may query the total available credits at a particular timestamp, or try to consume credits at a given time. Implement the `CreditSystem` class with these methods: - `addCredits(id: int, start: int, end: int, amount: int)`: adds a credit grant with the given unique id, valid from `start` to `end` inclusive, holding `amount` credits. If a grant with the same id already exists, the call is ignored. - `consumeCredits(timestamp: int, amount: int) -> bool`: tries to consume `amount` credits at `timestamp`. Credits come first from the active grant that expires soonest (smallest `end`, then smallest `id`). If the active grants hold fewer than `amount` credits in total, nothing is consumed and the call returns `false`. Otherwise it returns `true`. - `queryCredits(timestamp: int) -> int`: returns the total credits available at `timestamp` across all grants active at that time. ```hint All or nothing A failed `consumeCredits` must leave every grant unchanged. Work out whether the request can be met before you modify any balance. ``` ```hint Late and repeated calls A call with an older timestamp can arrive after newer ones, and a network retry can repeat an earlier `addCredits`. Check that any cleanup you do, such as dropping expired or empty grants, still gives the right answer in both cases. ``` ### Examples Example 1: ```text Input: operations = ["CreditSystem", "addCredits", "addCredits", "queryCredits", "consumeCredits", "queryCredits", "consumeCredits"] arguments = [[], [1, 1, 10, 50], [2, 3, 8, 30], [5], [5, 60], [5], [5, 30]] Output: [null, null, null, 80, true, 20, false] ``` Grant 1 (50 credits, valid 1 to 10) and grant 2 (30 credits, valid 3 to 8) are both active at time 5, so `queryCredits(5)` returns 80. `consumeCredits(5, 60)` draws from grant 2 first because it expires sooner (at 8): it takes all 30, then 30 from grant 1, and returns `true`. `queryCredits(5)` then returns 20, and `consumeCredits(5, 30)` returns `false` because only 20 are available. Example 2: ```text Input: operations = ["CreditSystem", "addCredits", "queryCredits", "consumeCredits", "queryCredits", "addCredits", "queryCredits"] arguments = [[], [10, 5, 15, 100], [5], [5, 40], [5], [10, 5, 15, 200], [5]] Output: [null, null, 100, true, 60, null, 60] ``` Grant 10 holds 100 credits valid 5 to 15, so `queryCredits(5)` returns 100. `consumeCredits(5, 40)` succeeds and leaves 60. The second `addCredits` with id 10 is ignored, so `queryCredits(5)` still returns 60. Example 3: ```text Input: operations = ["CreditSystem", "addCredits", "addCredits", "consumeCredits", "queryCredits", "consumeCredits", "addCredits", "queryCredits", "queryCredits"] arguments = [[], [1, 1, 5, 10], [2, 4, 20, 10], [10, 4], [4], [4, 12], [1, 1, 5, 10], [3], [15]] Output: [null, null, null, true, 16, true, null, 0, 4] ``` The consume at time 10 arrives first and can only draw from grant 2, since grant 1 ends at 5. Each grant has one balance across its whole window, so `queryCredits(4)` sees grant 1's 10 credits plus the 6 left in grant 2. `consumeCredits(4, 12)` empties grant 1 and takes 2 from grant 2. The repeated `addCredits` for id 1 is ignored even though that grant is used up, so `queryCredits(3)` returns 0 (grant 2 starts at 4) and `queryCredits(15)` returns the last 4 credits of grant 2. ### Constraints & Assumptions - `1 <= id <= 10^9` - `1 <= start <= end <= 10^9` - `1 <= amount <= 10^9` - `1 <= timestamp <= 10^9` - At most `10^4` calls in total across all methods. - A grant is active at timestamp `t` when `start <= t <= end`. - A grant keeps its id after its credits run out, so a later `addCredits` with that id is still ignored. ### Clarifying Questions to Ask - Does a grant have one balance shared across its whole window, so that spending from it at time 10 also lowers what a query at time 4 returns? - Is there any bound on how late a call can arrive, or can a call with a very old timestamp show up at any point? - Roughly how many grants are active at once, and how often are queries made compared with consumes? - Can the total for one timestamp exceed the 32-bit integer range, and what return type is expected? ### What a Strong Answer Covers - A grant record with its window and remaining balance, and how duplicate ids are detected, including a retry that arrives after the grant is used up. - The active-window test with inclusive bounds, and an all-or-nothing check before any balance changes. - Consumption in order of smallest `end` then smallest `id`, with a partial draw from the last grant touched. - Which cleanup is safe when calls arrive out of timestamp order. - Time and space per operation, and integer overflow for large totals. ### Follow-up Questions - How would you make `queryCredits` and `consumeCredits` faster than a scan over every grant when there are millions of grants? - What changes if a late consume must be charged as if it had arrived in timestamp order? - How would you bound memory if no call can arrive more than a fixed amount of time late? - How would you keep `consumeCredits` correct under concurrent calls, or with several servers serving the same account?

Overview: Design a GPU credit system that adds overlapping grants with time windows, consumes credits from the soonest-expiring grant first, and answers balance queries at any timestamp. It tests all-or-nothing updates, greedy ordering, idempotent adds and handling calls that arrive out of timestamp order.

OpenAI logo
OpenAI
Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

You are designing a system to manage GPU credits. Each credit grant has a unique id and is valid during a time window from start to end, inclusive. Credit grants may overlap in time.

Because of network delays, calls such as adding or consuming credits may arrive and be processed out of order with respect to their timestamps. Each call is applied to the current state when it arrives; its timestamp only decides which grants are active for that call. At any point you may query the total available credits at a particular timestamp, or try to consume credits at a given time.

Implement the CreditSystem class with these methods:

  • addCredits(id: int, start: int, end: int, amount: int) : adds a credit grant with the given unique id, valid from start to end inclusive, holding amount credits. If a grant with the same id already exists, the call is ignored.
  • consumeCredits(timestamp: int, amount: int) -> bool : tries to consume amount credits at timestamp . Credits come first from the active grant that expires soonest (smallest end , then smallest id ). If the active grants hold fewer than amount credits in total, nothing is consumed and the call returns false . Otherwise it returns true .
  • queryCredits(timestamp: int) -> int : returns the total credits available at timestamp across all grants active at that time.

Examples

Example 1:

Input:
operations = ["CreditSystem", "addCredits", "addCredits", "queryCredits", "consumeCredits", "queryCredits", "consumeCredits"]
arguments = [[], [1, 1, 10, 50], [2, 3, 8, 30], [5], [5, 60], [5], [5, 30]]

Output: [null, null, null, 80, true, 20, false]

Grant 1 (50 credits, valid 1 to 10) and grant 2 (30 credits, valid 3 to 8) are both active at time 5, so queryCredits(5) returns 80. consumeCredits(5, 60) draws from grant 2 first because it expires sooner (at 8): it takes all 30, then 30 from grant 1, and returns true. queryCredits(5) then returns 20, and consumeCredits(5, 30) returns false because only 20 are available.

Example 2:

Input:
operations = ["CreditSystem", "addCredits", "queryCredits", "consumeCredits", "queryCredits", "addCredits", "queryCredits"]
arguments = [[], [10, 5, 15, 100], [5], [5, 40], [5], [10, 5, 15, 200], [5]]

Output: [null, null, 100, true, 60, null, 60]

Grant 10 holds 100 credits valid 5 to 15, so queryCredits(5) returns 100. consumeCredits(5, 40) succeeds and leaves 60. The second addCredits with id 10 is ignored, so queryCredits(5) still returns 60.

Example 3:

Input:
operations = ["CreditSystem", "addCredits", "addCredits", "consumeCredits", "queryCredits", "consumeCredits", "addCredits", "queryCredits", "queryCredits"]
arguments = [[], [1, 1, 5, 10], [2, 4, 20, 10], [10, 4], [4], [4, 12], [1, 1, 5, 10], [3], [15]]

Output: [null, null, null, true, 16, true, null, 0, 4]

The consume at time 10 arrives first and can only draw from grant 2, since grant 1 ends at 5. Each grant has one balance across its whole window, so queryCredits(4) sees grant 1's 10 credits plus the 6 left in grant 2. consumeCredits(4, 12) empties grant 1 and takes 2 from grant 2. The repeated addCredits for id 1 is ignored even though that grant is used up, so queryCredits(3) returns 0 (grant 2 starts at 4) and queryCredits(15) returns the last 4 credits of grant 2.

Constraints & Assumptions

  • 1 <= id <= 10^9
  • 1 <= start <= end <= 10^9
  • 1 <= amount <= 10^9
  • 1 <= timestamp <= 10^9
  • At most 10^4 calls in total across all methods.
  • A grant is active at timestamp t when start <= t <= end .
  • A grant keeps its id after its credits run out, so a later addCredits with that id is still ignored.

Clarifying Questions to Ask Guidance

  • Does a grant have one balance shared across its whole window, so that spending from it at time 10 also lowers what a query at time 4 returns?
  • Is there any bound on how late a call can arrive, or can a call with a very old timestamp show up at any point?
  • Roughly how many grants are active at once, and how often are queries made compared with consumes?
  • Can the total for one timestamp exceed the 32-bit integer range, and what return type is expected?

What a Strong Answer Covers Guidance

  • A grant record with its window and remaining balance, and how duplicate ids are detected, including a retry that arrives after the grant is used up.
  • The active-window test with inclusive bounds, and an all-or-nothing check before any balance changes.
  • Consumption in order of smallest end then smallest id , with a partial draw from the last grant touched.
  • Which cleanup is safe when calls arrive out of timestamp order.
  • Time and space per operation, and integer overflow for large totals.

Follow-up Questions Guidance

  • How would you make queryCredits and consumeCredits faster than a scan over every grant when there are millions of grants?
  • What changes if a late consume must be charged as if it had arrived in timestamp order?
  • How would you bound memory if no call can arrive more than a fixed amount of time late?
  • How would you keep consumeCredits correct under concurrent calls, or with several servers serving the same account?