I knew the standard LRU cache with a doubly linked list and hashmap cold.
Start by clarifying requirements and constraints, then propose a design using a hash map for O(1) lookups and a doubly linked list for LRU ordering. Explain how to handle TTL and eviction, emphasizing the preference for removing expired entries first. Finally, discuss implementation details, edge cases, and trade-offs.
Pro tip: Mention that you would use a background thread or lazy expiration to clean up expired entries, but ensure that get operations check TTL to never return expired data. This shows awareness of concurrency and performance trade-offs.
Ask about expected load, concurrency needs, memory limits, and whether TTL is per-entry or global. Confirm that O(1) average for get and put is required.
Use an unordered_map (hash map) for O(1) key lookup, mapping to nodes in a doubly linked list that maintains LRU order. Each node stores key, value, expiry timestamp, and pointers.
For get: check if key exists and not expired; if expired, remove and return miss; else move node to front (most recently used) and return value. For put: if key exists, update value and expiry, move to front; else insert new node at front, and if capacity exceeded, evict.
When evicting, first scan from the back (least recently used) to find an expired entry; if found, remove it. If no expired entries, evict the LRU node. To keep O(1), consider maintaining a separate list or queue of expired entries, or accept O(n) worst-case for eviction but argue it's amortized O(1) if expirations are frequent.
Mention thread-safety (e.g., mutex or sharding), memory overhead, and alternative eviction policies. Highlight that lazy expiration on get ensures expired entries are never returned.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.