coding
OpenAI
Google
AWS

OpenAI Coding Interview: Time-Based GPU Credit System

Topics:
Sweep Line
Event Sourcing
Simulation
Roles:
Software Engineer
Backend Engineer
Systems Engineer
Experience:
Entry Level
Mid Level
Senior

Question Description

Implement a replayable, time-based credit tracking service that models GPU credits valid on half-open intervals [start, expiration). You must support three operations: record credit grants (adds), attempt charges (consumes credits at an event timestamp), and query the balance at an arbitrary timestamp. All operations are driven by event timestamps and the system must tolerate out-of-order arrival: queries consider only events with timestamp ≤ query time.

During a balance query you must replay cached events from the first recorded event up to the query time, applying adds as grants valid on [start, expiration) and applying charges by consuming from grants that are added ≤ charge time and unexpired at that time. When consuming, prefer grants that expire earliest. Charges that would make balance negative must fail (return None) and not modify state for events ≤ that timestamp. If replay finds recorded charges that cannot be reconciled with available grants, get_balance must raise an exception.

Flow you should expect in an interview: specify method signatures and side effects, describe data structures for cached events and grant bookkeeping, implement correct replay semantics, add persistence/restore, then discuss optimizations and trade-offs.

Skill signals: demonstrate event-sourcing thinking, sweep-line / interval handling, greedy consumption by earliest-expiry, correctness under out-of-order events, deterministic persistence format, and trade-offs for scaling (snapshotting, indexing, incremental aggregates). Boldly document any public structures and the persisted byte format so the harness can restore identical semantics.

Common Follow-up Questions

  • How would you optimize get_balance for very large event histories (millions of events)? Describe snapshotting, incremental aggregates, or indexing strategies and their correctness trade-offs.
  • Design an efficient persisted format and restore procedure that guarantees identical semantics while minimizing restore time. How do you handle schema upgrades and backward compatibility?
  • How would you modify the system to support partial refunds or reversal events that arrive out of order? Explain replay and reconciliation changes.
  • If multiple events could occur at the same timestamp (contrary to the constraint), how would you define deterministic tie-breaking and update the replay logic?
  • How would you extend the system to support multi-tenant isolation (per-customer credit pools) and global queries across tenants while preserving replay correctness?

Related Questions

1Design an event-sourced billing system that charges users based on usage windows and supports out-of-order events.
2Implement a sliding-window or token-bucket rate limiter that prefers earliest-expiring tokens when consuming.
3Simulate resource leases with start/duration/expiration and support replayable state reconstruction.
4How to persist and restore event logs with snapshots for fast recovery while guaranteeing no loss of determinism?

Explore More Questions

Practice This Question with AI

Get real-time hints, detailed requirements, and insightful analysis of the question.

OpenAI Coding: Time-Based GPU Credit System Design | Voker