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]
Input: ([('grant', 'u1', 4, 10), ('grant', 'u1', 3, 7), ('consume', 'u1', 5, 2), ('refund', 'u1', 4, 8), ('balance', 'u1', 8)],)
Expected Output: [True, 2, 4]
Input: ([('grant', 'a', 5, 5), ('grant', 'b', 4, 6), ('balance', 'a', 5), ('balance', 'b', 5), ('consume', 'a', 1, 5), ('consume', 'b', 4, 6), ('balance', 'b', 6)],)
Expected Output: [0, 4, False, False, 0]
Input: ([('balance', 'nobody', 0), ('consume', 'nobody', 1, 0), ('refund', 'nobody', 5, 0)],)
Expected Output: [0, False, 0]
Input: ([('grant', 'u', 1000000000000000000, 100), ('grant', 'u', 1000000000000000000, 200), ('consume', 'u', 1500000000000000000, 10), ('balance', 'u', 10), ('refund', 'u', 1000000000000000000, 10), ('balance', 'u', 10)],)
Expected Output:
Input: ([('grant', 'u', 5, 10), ('consume', 'u', 5, 2), ('refund', 'u', 5, 10), ('balance', 'u', 10)],)
Expected Output:
Input: ([('grant', 'a', 10, 100), ('consume', 'a', 3, 1), ('consume', 'a', 4, 1), ('refund', 'a', 5, 1), ('balance', 'a', 1)],)
Expected Output:
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.