Implement a GPU credit manager
Company: OpenAI
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of concurrent resource management, advanced data structures for top-K and per-user queries, enforcement of caps and expirations, and algorithmic complexity guarantees while maintaining atomicity under concurrent operations.
Constraints
- 0 <= len(operations) <= 2 * 10^5
- Operation timestamps are in nondecreasing order
- 0 <= amount, cap <= 10^9
- User and organization names are non-empty strings
- If `expire_time` is not `None`, it is an integer timestamp; a lot with `expire_time <= t` is considered expired before time `t` operations run
Examples
Input: ({}, {}, {}, [])
Expected Output: []
Explanation: With no operations, there are no results to return.
Input: ({'alice': 10, 'bob': 7}, {'alice': 'org1', 'bob': 'org1', 'carol': 'org2'}, {'org1': 12, 'org2': 20}, [('grant', 1, 'alice', 8, 5), ('grant', 2, 'bob', 7, None), ('consume', 3, 'alice', 3), ('refund', 4, 'bob', 5), ('get', 4, 'bob'), ('topK', 4, 2), ('get', 5, 'alice'), ('topK', 6, 3)])
Expected Output: [8, 4, True, 3, 7, [['bob', 7], ['alice', 5]], 0, [['bob', 7]]]
Explanation: Bob's first grant is limited by the organization cap, Bob's refund is also capped, and Alice's remaining expiring credits disappear at time 5.
Input: ({'u': 20}, {}, {}, [('grant', 1, 'u', 5, 10), ('grant', 2, 'u', 7, 6), ('consume', 3, 'u', 6), ('get', 5, 'u'), ('get', 6, 'u'), ('topK', 6, 1)])
Expected Output: [5, 7, True, 6, 5, [['u', 5]]]
Explanation: Consumption uses the earliest-expiring lot first, leaving 1 credit in the lot expiring at time 6, which then expires before the `get` at time 6.
Input: ({'a': 10, 'b': 10, 'c': 10}, {'a': 'o', 'b': 'o', 'c': 'o2'}, {'o': 9, 'o2': 10}, [('grant', 1, 'a', 5, None), ('grant', 1, 'b', 5, None), ('grant', 1, 'c', 5, None), ('topK', 1, 3), ('consume', 2, 'a', 2), ('refund', 3, 'b', 10), ('topK', 3, 3)])
Expected Output: [5, 4, 5, [['a', 5], ['c', 5], ['b', 4]], True, 2, [['b', 6], ['c', 5], ['a', 3]]]
Explanation: User `b` is limited by the organization cap both on the initial grant and on the later refund. In the first `topK`, `a` and `c` tie at 5, so `a` comes first lexicographically.
Input: ({'x': 5}, {}, {}, [('topK', 0, 2), ('consume', 1, 'x', 1), ('refund', 2, 'x', 4), ('grant', 3, 'x', 3, None), ('consume', 4, 'x', 5), ('get', 5, 'x')])
Expected Output: [[], False, 4, 1, True, 0]
Explanation: This covers an empty `topK`, a failed consume on a zero balance, and a grant that is partially applied because user `x` is already near the cap.
Hints
- You need two different orderings at once: earliest expiration for consuming/expiring lots, and largest current balance for `topK`. Heaps plus hash maps are a strong fit.
- For `topK`, avoid updating heap entries in place. Push new balance entries and skip stale ones later with lazy deletion.