Design a token manager with lazy expiration
Company: Citadel
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates time-based data structure design and algorithmic reasoning for managing expiring items, focusing on hashing, timestamp ordering and lazy deletion semantics. It is commonly asked in Coding & Algorithms interviews to assess practical application skills in designing efficient, scalable token/TTL managers under performance constraints rather than purely conceptual theory.
Constraints
- 1 <= timeToLive <= 10^8
- 0 <= len(operations) <= 2 * 10^5
- 1 <= currentTime <= 10^9
- Operation times are given in non-decreasing order of currentTime
- 1 <= len(tokenId) <= 20, and tokenId contains only letters and digits
Examples
Input: (5, [("generate", "abc", 1), ("renew", "abc", 5), ("count", 6), ("count", 10)])
Expected Output: [1, 0]
Explanation: The token `abc` is first valid until 6, then renewed at time 5 so it becomes valid until 10. It is still valid at time 6, but expired at time 10 because the interval is half-open.
Input: (3, [("generate", "a", 1), ("generate", "a", 2), ("count", 3), ("count", 4), ("count", 5)])
Expected Output: [1, 1, 0]
Explanation: The second generate overwrites token `a` with a later expiry. The old heap entry becomes stale and must be ignored during lazy cleanup.
Input: (4, [("generate", "x", 1), ("generate", "y", 2), ("renew", "x", 5), ("renew", "y", 5), ("count", 5), ("count", 6), ("count", 9)])
Expected Output: [1, 1, 0]
Explanation: `x` expires exactly at time 5, so renewing it at time 5 does nothing. `y` is still valid at time 5 and gets renewed to expire at time 9.
Input: (10, [])
Expected Output: []
Explanation: No operations means there are no count results.
Input: (2, [("generate", "z", 1), ("renew", "z", 2), ("renew", "z", 3), ("count", 3), ("count", 4), ("count", 5)])
Expected Output: [1, 1, 0]
Explanation: The same token is renewed multiple times, creating multiple stale expiry records in the heap. Only the newest expiry should be considered valid.
Hints
- Store the latest expiry time for each token in a hash map.
- A min-heap of `(expiryTime, tokenId)` lets you lazily discard expired tokens. When popping from the heap, ignore entries that are stale because the token was renewed or overwritten later.