← Bytedance Interview Insights
I knew the LRU part cold, hashmap plus doubly linked list, done that a dozen times.
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. Explain how to handle TTL with lazy expiration and discuss trade-offs of different expiration strategies.
Pro tip: Mention that lazy expiration alone can lead to memory bloat, so consider a hybrid approach with periodic cleanup or a background thread, but keep the core operations O(1).
Ask about cache size limits, concurrency needs, and whether expiration should be strictly lazy or can include active cleanup. Confirm that get/put must be O(1) average time.
Use a hash map (dictionary) for O(1) key lookup, mapping to nodes in a doubly linked list that maintains LRU order. Each node stores key, value, expiration timestamp, and prev/next pointers.
On get, move the accessed node to the front (most recently used). On put, if key exists, update value and move to front; if new, add to front and evict the least recently used (tail) if capacity exceeded.
Store expiration time per entry. On get, check if expired; if so, remove the entry and return null. On put, set expiration time. Optionally, discuss active expiration strategies for memory efficiency.
Confirm O(1) average time for get/put. Discuss trade-offs: lazy expiration saves CPU but may use more memory; active expiration adds overhead but keeps memory bounded. Mention concurrency considerations if needed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.