I knew OrderedDict the second they said LRU, but the TTL part made me second-guess my whole structure for a bit.
Start by clarifying requirements and constraints, then propose a design using a hash map for O(1) access and a doubly linked list for LRU ordering, with each entry storing an expiration timestamp. Explain how to handle expiration lazily on access and proactively during eviction, and discuss trade-offs between different expiration strategies.
Pro tip: Mention that you would use a monotonic clock (e.g., System.nanoTime() in Java) for TTL to avoid issues with system clock adjustments, and consider a background thread for proactive expiration if the workload is read-heavy.
Ask about expected cache size, read/write ratio, TTL granularity, and whether expired entries should be removed eagerly or lazily. Confirm that get returns -1 for missing/expired and put accepts TTL in seconds.
Propose a hash map for O(1) key lookup and a doubly linked list to maintain LRU order. Each node stores key, value, expiration timestamp, and pointers to prev/next.
For get: check if key exists and if expired; if expired, remove and return -1; else move node to front (most recently used) and return value. For put: if key exists, update value and TTL and move to front; else insert new node at front; if capacity exceeded, evict expired entries first, then LRU.
Explain lazy expiration on access and proactive expiration during eviction. Discuss trade-offs: lazy expiration may keep expired entries until accessed, while proactive expiration (e.g., background thread) adds complexity but reduces memory pressure.
State that get and put are O(1) average time. Discuss trade-offs: memory overhead of storing expiration timestamps, choice of clock, and potential need for synchronization in concurrent scenarios.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.