I knew LRU cache cold, hashmap plus doubly linked list, move to front on access, evict from the tail.
Start by clarifying requirements and edge cases, then propose a hash map combined with a doubly linked list to track recency and sizes. Explain how eviction works by removing from the tail until there's enough capacity, and handle the put rejection when item size exceeds total capacity.
Pro tip: Mention that you can optimize eviction by maintaining a running total of used capacity and only evicting when necessary, avoiding O(n) scans. Also, discuss thread-safety considerations if the cache is shared, showing awareness of concurrent environments.
Ask about expected operations, size constraints, concurrency needs, and behavior when item size exceeds capacity. Confirm that get should update recency and put should reject oversized items.
Propose a hash map for O(1) access to nodes and a doubly linked list to maintain recency order, with each node storing key, value, and size. Track total used capacity.
For get: if key exists, move node to front (most recent) and return value; else return null. For put: if key exists, update value and size, adjust capacity, move to front; else create new node, add to front, and evict from tail until enough space.
During put, if item size > total capacity, reject immediately. Otherwise, while used capacity + item size > total capacity, remove tail node (least recent) and update used capacity. Then insert new item.
Explain that both get and put are O(1) amortized due to hash map and linked list operations. Discuss potential optimizations like lazy eviction or using a priority queue, and trade-offs with concurrency.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.