Implement an expiring GPU-credit manager
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of efficient data structures, algorithmic complexity, concurrency control, and time-based resource accounting required to implement an expiring GPU-credit manager.
Constraints
- 1 <= len(operations) <= 200000
- 1 <= amount <= 10^18
- 0 <= expiresAt, now, atTime <= 10^18
- Timestamped operations (consume, balance, refund) appear in nondecreasing time order in the input
- Each grant is issued before it expires
- A grant is expired when currentTime >= expiresAt
Examples
Input: ([('grant', 'alice', 10, 6), ('grant', 'alice', 5, 10), ('balance', 'alice', 1), ('consume', 'alice', 12, 2), ('balance', 'alice', 2), ('refund', 'alice', 4, 4), ('balance', 'alice', 4), ('consume', 'alice', 8, 5), ('balance', 'alice', 5)],)
Expected Output: [15, True, 3, 4, 7, False, 7]
Input: ([('grant', 'bob', 5, 3), ('consume', 'bob', 5, 1), ('refund', 'bob', 5, 3), ('balance', 'bob', 3)],)
Expected Output: [True, 0, 0]
Hints
- For each user, keep grants ordered by expiration so the earliest-expiring grant is always chosen first during consume.
- A stack of consumption chunks is a natural fit for refund, because refund must undo the most recent successful consumptions first.