Implement GPU credit ledger
OpenAISoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's ability to design efficient stateful systems and algorithmic data structures for tracking per-tenant resource balances, including handling idempotency, negative balance policies, concurrency, and persistence.
You manage a GPU pool with per-tenant credits. Given a list/stream of events where each event adjusts a tenant’s credit balance by a signed integer (positive = add credit, negative = deduct credit), implement three functions: (
- init(events): initialize internal state by applying the given ordered events; (
- getBalance(tenantId): return the current credit balance for the specified tenant; (
- applyEvent(event): process a new single event and update state. Define the event schema (tenantId, delta, optional timestamp/id), decide whether to reject events that would drive balances below zero or allow negatives, and handle idempotency if event ids repeat. Aim for O(
- average time per operation after O(n) initialization, and discuss data structures, concurrency considerations, and how you would persist state for crash recovery.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.