This was a level 3 extension of a simpler key-value store problem, so you had to build on top of existing add/remove/scan logic and layer in expiration semantics.
Start by clarifying requirements and constraints, then outline a high-level design using a hash map for storage and a min-heap or timing wheel for TTL management. Discuss read/scan logic to filter expired records, and cover concurrency, persistence, and performance trade-offs.
Pro tip: Mention that you would use a monotonic clock to avoid issues with system time changes, and discuss how to handle TTL expiration lazily versus actively to balance latency and memory usage.
Ask about expected scale, read/write patterns, TTL granularity, consistency needs, and whether persistence is required. Confirm that expired records should be invisible immediately after TTL.
Propose a record structure with key, value, write timestamp, and TTL. Use an in-memory hash map for O(1) key access, and consider a secondary index for scans (e.g., sorted by key or timestamp).
Choose between lazy expiration (check on read) and active expiration (background sweeper). Discuss using a min-heap or timing wheel to efficiently track and evict expired records.
For reads, check TTL and return null if expired. For scans, iterate over records and filter out expired ones, possibly using a snapshot or consistent view to avoid concurrent modification issues.
Discuss thread-safety using locks or concurrent data structures, and trade-offs between read/write locks, lock-free approaches, and sharding. Mention memory management and potential eviction policies.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.