r/OfferEngineering • u/Aoki_zhang • 3d ago
Coding Question OpenAI & Perplexity Coding Screen Problem - Cloud Credit Balance Replay
Problem Description
A cloud platform gives customers GPU credits through time-limited grants. Each grant has:
a unique grant ID
an amount of credits
a start time
an expiration time
A grant is active during: [startTime, expirationTime). This means it is usable at startTime, but not usable at expirationTime.
The system also receives credit usage events. A usage event consumes credits at a specific timestamp. Events may arrive out of order. For example, the system may receive a usage event first and a grant event later, even if the grant started earlier. So every balance query must replay all currently known events in chronological order.
When consuming credits at time t:
only grants active at time
tmay be usedcredits should be consumed from the grant that expires soonest first
if the usage cannot be fully covered, the query should return
-1
Implement the CloudCreditSystem class:
CloudCreditSystem()initializes an empty credit system.void addGrant(String grantId, int amount, int startTime, int expirationTime)adds a credit grant.void useCredits(int amount, int timestamp)records a credit usage event.int getBalance(int timestamp)returns the total remaining available credits attimestampafter replaying all known grants and usages up to that time. If any usage event up totimestampcannot be fully covered, return-1.
Rules:
Grant and usage events may arrive in any order
No two mutation operations share the same timestamp
Grants can overlap
Usage consumes active credits with earliest expiration first
A grant is expired when
expirationTime <= timestampQuery results may change after later events are added
Constraints:
All amounts and times are in
[0, 1000]grantIdis uniqueNumber of operations can be large enough that each query should process events efficiently
A grant is active on
[startTime, expirationTime)
Want to practice it before interview?
You can practice it and view solution code at here.
Preparing for your next interview?
Chill Interview tracks recent interview experiences and recurring question patterns across top companies at here.