This one took me a second to settle into because the TTL layer adds just enough wrinkle to throw off your default LRU muscle memory.
Start by clarifying requirements (TTL semantics, capacity, thread-safety) and then propose a design combining a hash map for O(1) key lookup with a doubly linked list for O(1) recency updates. Explain how to handle expiration lazily on access and proactively during eviction, and discuss trade-offs between different approaches.
Pro tip: Mention that lazy expiration alone can cause memory bloat and that a background thread or periodic cleanup is often needed in production; also note that TTL should be set at insertion and not refreshed on access unless specified.
Ask about TTL semantics (absolute vs sliding), capacity limits, thread-safety needs, and expected performance. Confirm whether expired entries should be removed immediately or lazily.
Use a hash map (dictionary) for O(1) key lookup and a doubly linked list to maintain recency order. Each node stores key, value, and expiration timestamp.
For get: check existence and expiration; if valid, move node to front (most recent) and return value. For put: insert or update, set TTL, move to front, and evict LRU non-expired entry if at capacity.
Explain that get and put are O(1) average time due to hash map and linked list operations. Discuss trade-offs: lazy expiration vs proactive cleanup, memory overhead, and thread-safety considerations.
Address edge cases: expired entries during eviction, updating TTL on put, and handling capacity 0. Mention potential optimizations like using a min-heap for TTL or a timing wheel for efficient expiration.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.