Implement an expiring GPU-credit manager for a cloud provider. Each user receives credit grants with an amount and an expiration timestamp. Support:
(
-
grant(userId, amount, expiresAt) to add a new grant;
(
-
consume(userId, amount) to atomically deduct credits using earliest-expiring-first semantics and return true/false;
(
-
balance(userId, atTime=now) to report total unexpired credits; and
(
-
optional refund(userId, amount) that restores most-recently-consumed credits before expiration. Requirements: O(log n) per operation where n is the number of active grants for that user; expired grants must not be consumable; partial consumption across multiple grants must be handled; insufficient balance must not change state. Discuss data structures (e.g., heaps/trees), concurrency control for parallel consume calls, time handling, and include unit tests for edge cases (exact expiry boundaries, large amounts, empty state).