Design a Credit Tracker with Expirations and Earliest-Expiry-First Spending
Company: Perplexity
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a `CreditTracker` that manages a user's credit balance where each credit deposit has an expiration time. Credits that are not spent before their expiration time are permanently lost.
When the user spends credits, the system must consume credits in earliest-expiry-first order (the credits closest to expiring are used before credits that expire later). Expired credits are never spent.
All operations are timestamped, and timestamps are strictly increasing across calls.
Implement the following methods:
- `addCredits(timestamp: int, amount: int, expiryTime: int)`: Add `amount` credits at `timestamp` that expire at time `expiryTime` (credits become unusable when the current time is `>= expiryTime`).
- `spend(timestamp: int, amount: int) -> int`: Attempt to spend `amount` credits at `timestamp`, consuming non-expired credits in earliest-expiry-first order. Returns the actual amount spent, which may be less than `amount` if there are not enough non-expired credits.
- `getBalance(timestamp: int) -> int`: Return the total non-expired credit balance at `timestamp`.
Example 1:
```text
Input:
["CreditTracker", "addCredits", "addCredits", "getBalance", "spend", "getBalance", "getBalance", "spend"]
[[], [1, 100, 10], [2, 50, 5], [3], [4, 70], [6], [11], [12, 10]]
Output: [null, null, null, 150, 70, 80, 0, 0]
Explanation:
tracker = CreditTracker() # empty tracker
tracker.addCredits(1, 100, 10) # +100, expires at t=10
tracker.addCredits(2, 50, 5) # +50, expires at t=5
tracker.getBalance(3) # 150 (both buckets live)
tracker.spend(4, 70) # 70 (50 from expiry=5, 20 from expiry=10)
tracker.getBalance(6) # 80 (expiry=5 bucket exhausted, 80 left)
tracker.getBalance(11) # 0 (expiry=10 bucket has now expired)
tracker.spend(12, 10) # 0 (no credits left)
```
Example 2:
```text
Input:
["CreditTracker", "addCredits", "spend", "addCredits", "spend", "getBalance"]
[[], [1, 20, 100], [2, 5], [3, 30, 50], [4, 40], [5]]
Output: [null, null, 5, null, 40, 5]
Explanation:
tracker = CreditTracker() # empty tracker
tracker.addCredits(1, 20, 100) # +20, expires at t=100
tracker.spend(2, 5) # 5 (15 remain in expiry=100 bucket)
tracker.addCredits(3, 30, 50) # +30, expires at t=50
tracker.spend(4, 40) # 40 (30 from expiry=50, 10 from expiry=100)
tracker.getBalance(5) # 5 (5 credits remain in expiry=100 bucket)
```
```hint Which bucket matters
Both spending and expiring act on the bucket with the smallest expiry time. Pick a structure that hands you that bucket cheaply after any number of inserts.
```
```hint Time only moves forward
Once a bucket has expired it stays expired, so you can clean up at the start of each call instead of on a timer. Think about how to answer a balance query without adding up every bucket each time.
```
### Constraints & Assumptions
- `1 <= amount <= 10^9`
- `1 <= timestamp <= 10^9`
- `1 <= expiryTime <= 10^9`
- `timestamp` values are strictly increasing across calls.
- At most `10^5` calls total across all methods.
### Clarifying Questions to Ask
- At the exact moment `timestamp == expiryTime`, can those credits still be spent?
- If a spend asks for more than the live balance, should it take what is available or reject the whole request?
- What should `addCredits` do with a deposit whose `expiryTime` is at or before its own `timestamp`?
- Can two calls share a timestamp, or are timestamps always strictly increasing?
### What a Strong Answer Covers
- A data structure that returns the earliest-expiring live bucket quickly after inserts and partial spends, with the cost of each operation.
- When and how expired buckets are removed, applying the `>= expiryTime` boundary the same way in all three methods.
- Partial spends: splitting a bucket, returning less than requested, and keeping the balance consistent with the buckets.
- Amortized time and space complexity over the full sequence of calls.
- Edge cases such as deposits that are already expired, spends after everything expired, and totals that overflow 32-bit integers.
### Follow-up Questions
- Timestamps can now arrive in any order. Which parts of your design break, and what would you replace them with?
- Product wants `spend` to be all-or-nothing: either the full amount is charged or nothing is. What changes?
- How would you answer "how many credits expire before time T" without scanning every bucket?
- Many users share one service and calls for the same user can arrive concurrently. How do you keep each balance correct?
Overview: Implement a CreditTracker that stores credit deposits with expiration times, spends non-expired credits in earliest-expiry-first order, and reports the live balance at strictly increasing timestamps. It tests data structure choice, expiry boundary handling, partial spends and amortized complexity analysis.