Design a token credit tracker with historical balance queries
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a class `TokenCreditTracker` that tracks token credits for multiple users over time. The system should support adding credits, deducting credits, and querying a user's balance at any specific historical timestamp.
Each credit operation occurs at a given `timestamp`. When querying, the system should return the user's balance as it was at or before the queried timestamp. If a user has no recorded transaction at or before the queried timestamp, return `0`.
You may assume that for any given user, the timestamps of `add` and `deduct` operations are strictly increasing. A `deduct` operation will never cause a user's balance to go below `0`.
Implement the following methods:
- `add(user_id: str, timestamp: int, amount: int) -> None`: add the specified amount of token credits to the user's balance at the given timestamp.
- `deduct(user_id: str, timestamp: int, amount: int) -> None`: deduct the specified amount of token credits from the user's balance at the given timestamp.
- `query(user_id: str, timestamp: int) -> int`: return the user's token credit balance at or before the given timestamp, or `0` if no record exists at or before that time.
Example 1:
```text
Input:
["TokenCreditTracker", "add", "add", "query", "deduct", "query", "query"]
[[], ["alice", 1, 100], ["alice", 5, 50], ["alice", 5], ["alice", 10, 30], ["alice", 10], ["alice", 3]]
Output: [null, null, null, 150, null, 120, 100]
Explanation:
tracker = TokenCreditTracker()
tracker.add("alice", 1, 100) # alice balance: 100
tracker.add("alice", 5, 50) # alice balance: 150
tracker.query("alice", 5) # returns 150 (balance at timestamp 5)
tracker.deduct("alice", 10, 30) # alice balance: 120
tracker.query("alice", 10) # returns 120 (balance at timestamp 10)
tracker.query("alice", 3) # returns 100 (timestamp 1 is the latest record at or before 3)
```
Example 2:
```text
Input:
["TokenCreditTracker", "query", "add", "query", "query", "add", "query"]
[[], ["bob", 5], ["bob", 10, 200], ["bob", 10], ["bob", 9], ["bob", 20, 100], ["bob", 15]]
Output: [null, 0, null, 200, 0, null, 200]
Explanation:
tracker = TokenCreditTracker()
tracker.query("bob", 5) # returns 0 (no record for bob yet)
tracker.add("bob", 10, 200) # bob balance: 200
tracker.query("bob", 10) # returns 200 (balance at timestamp 10)
tracker.query("bob", 9) # returns 0 (no record at or before timestamp 9)
tracker.add("bob", 20, 100) # bob balance: 300
tracker.query("bob", 15) # returns 200 (latest record at or before 15 is timestamp 10)
```
```hint What to store per operation
Think about what each stored record should hold so that answering a query never requires replaying a user's earlier operations.
```
```hint Use the ordering guarantee
Each user's operation timestamps only go up. Ask what that implies about the order of that user's records, and how quickly you can then find the right record for an arbitrary query time.
```
### Constraints & Assumptions
- `1 <= user_id.length <= 20`
- All `user_id` values consist of lowercase English letters and digits.
- `1 <= timestamp <= 10^9`
- `1 <= amount <= 10^6`
- Timestamps for `add` and `deduct` operations for a given user are strictly increasing.
- `deduct` will never cause a user's balance to go below `0`.
- At most `10^4` calls will be made in total.
### What a Strong Answer Covers
- A per-user storage layout, what each record holds, and the cost of answering a query from it.
- How the strictly increasing timestamps keep each user's records ordered without any sorting on insert.
- The search for the right record, including an exact timestamp match, a time before the first record, and a time after the last record.
- The zero-balance cases (unknown user, query before the first operation) handled without side effects on reads.
- Time and space complexity per method, and the largest balance the constraints allow.
### Follow-up Questions
- How would the design change if operations for a user could arrive with timestamps out of order?
- If a `deduct` could exceed the available balance, how would you reject it, and what should `query` report afterwards?
- How would you support credits that expire a fixed time after they are added?
- How would you persist the history or bound its memory when a user accumulates millions of operations?
Overview: Design a class that records token credit additions and deductions for many users and returns any user's balance as of a past timestamp. It tests choosing a per-user history layout, searching sorted timestamps efficiently, and handling boundary cases such as queries before a user's first operation.