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.