I jumped straight to a hash map and felt good about it for about 30 seconds.
Start by clarifying requirements (read/write ratio, latency, memory constraints) and then propose a hash map storing token metadata (value, expiry timestamp). Explain that lookups check expiry and treat expired tokens as absent, while cleanup is amortized via probabilistic or incremental strategies like periodic sweeps or lazy deletion with a background thread. Discuss trade-offs between eager vs lazy cleanup and how to bound memory growth.
Pro tip: Mention that in a real system you'd combine lazy deletion with a bounded cleanup mechanism (e.g., sampling or a min-heap) to avoid unbounded memory growth, and that you'd monitor expired token ratio to tune cleanup frequency.
Ask about read/write patterns, token volume, acceptable latency, memory limits, and whether persistence or distribution is needed. This shapes the data structure and cleanup strategy.
Use a hash map (or concurrent map) keyed by token ID, storing value and expiration timestamp. On lookup, compare current time to expiry; if expired, return invalid without deleting immediately.
When a lookup finds an expired token, optionally delete it then (lazy deletion) to free memory opportunistically. This spreads cleanup cost across reads.
Add a periodic background job that samples or scans a portion of entries and removes expired ones, or use a min-heap of expirations to efficiently find expired tokens. This bounds memory without blocking reads.
Compare eager vs lazy cleanup: eager uses more CPU but keeps memory low; lazy is faster but risks memory bloat. For distributed systems, consider sharding and per-shard cleanup.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.