AO
Back

GPU Credit Management System

CodingPhone, OnsiteSoftware Engineer, Machine Learning EngineerLast reported June 2026High Frequency

Problem Overview

Implement a GPU credit management system with the following three methods: 1.
Full problem statement
Implement a GPU credit management system with the following three methods: 1. `add_credit(grant_id, amount, timestamp, expiration)` — Add a credit grant identified by `grant_id` with a given `amount`, valid starting from `timestamp` and expiring at `expiration` (exclusive, i.e., the credit is invalid at or after `expiration`). Multiple grants can overlap in time. 2. `subtract(amount, timestamp)` (also called `charge`, `use_credit`) — Deduct the given `amount` of credits as of the given `timestamp`. When consuming credits, prioritize grants that expire soonest first. The deduction affects future available credit (i.e., if grant A expires at t=40 and grant B at t=70, a subtract at t=30 drains A first). Operations may arrive out of order (timestamps need not be monotonically increasing), so computation may be deferred until `get_balance` is called. 3. `get_balance(timestamp)` — Return the credit balance at the given `timestamp`. Expired credits (those whose expiration ≤ timestamp) are not counted. If the balance at that point would be negative (i.e., subtracts exceed available credits), return `None` (or throw an `InsufficientCreditException`). Return `0` if no subtract has occurred but no active credit covers that timestamp. **Key behaviors / edge cases:** - Credits are time-bounded: a credit added with `timestamp=10, expiration=40` is active during [10, 40) (right-exclusive). - `subtract` deducts from the earliest-expiring active grants first (greedy, min-heap by expiration). - A `subtract` that reduces a grant reduces that grant's remaining amount and propagates to later-expiring grants if needed. - `get_balance(ts)` should reflect the state as of `ts`: only grants whose start ≤ ts < expiration count; prior subtracts that occurred at ts' ≤ ts count; past negative states (i.e., a prior `get_balance` returned `None`) propagate — all subsequent `get_balance` calls also return `None`. - All `add_credit` and `subtract` calls may be provided in any order; the system must reconstruct the correct timeline at query time. - No time-complexity optimization is required; brute-force (recompute from scratch on each `get_balance`) is acceptable. **Variant I (simpler):** `subtract` deducts only from currently available credits and does not affect future credit amounts — use a sweep-line to sum all credit intervals, then subtract usage. **Variant II (harder, more common):** `subtract` deducts from active grants in earliest-expiration order and permanently reduces those grant amounts, affecting future queries.

Follow-up Arc

Interviewers escalate through these phases. The order varies, but most candidates see at least one from each bucket.
Concurrency · 1Edge cases · 1Trade-off discussion · 2
Concurrency
How would you handle concurrency — multiple threads calling add_credit, subtract, and get_balance simultaneously?
Probes for: Candidate has a working solution
Edge cases
How would you design this as a production system to control credit usage per customer tier (e.g., rate limiting)?
Probes for: After correctness discussion
Trade-off discussion
How would you optimize performance if get_balance is called very frequently?
Probes for: Candidate finishes the base implementation
What if the matrix/dataset is very large — how would you scale this?
Probes for: Candidate's system handles in-order events correctly

Approach Trade-offs

Approaches actually attempted in reports — including ones that lost candidates time. Pick deliberately.
ApproachNotes
Sweep-line (Variant I only)For the simpler variant where subtract doesn't affect future grants, collect all add-credit intervals and subtract events, sort by timestamp, and compute running balance using a sweep. Much simpler but incorrect for Variant II where subtracts must reduce specific grant amounts.
Event log with lazy evaluationAppend all add/subtract events into a sorted event list; only compute at get_balance time. Straightforward and correct for out-of-order inputs, but O(N log N) per query due to re-sorting. Acceptable since interviewers explicitly say performance is not a concern.
Append expiration as negative eventsModel each grant as two events: (start, +amount) and (expiration, -amount). Process subtract by adjusting expiration events. Elegant conceptually but tricky to implement correctly when subtracts span multiple grants.

Practice

Open the editor to write a solution against test cases, then return here to compare against the follow-ups.
Open Editor →

More OpenAI Questions

Free preview

Every question in the OpenAI catalog gets this depth

What you just read — canonical solution, follow-up arc, what passing candidates actually did — exists for all 97 OpenAI questions, refreshed monthly from new candidate reports.

$49/mo founding price for the first 100 subscribers · $79/mo after · cancel anytime
OpenAI · Coding · Reported 43× across candidate reports