Knew the LRU cache pattern cold so the base structure came quickly, hashmap plus doubly linked list.
Start by clarifying requirements and edge cases, then propose a design using a hash map for O(1) key lookup and a doubly linked list to maintain LRU order. Explain how to track total weight and handle evictions, including the rejection of overweight entries, and discuss trade-offs and potential optimizations.
Pro tip: Emphasize the importance of thread safety and concurrency, as Netflix operates at scale; mention how you would handle concurrent access with locks or lock-free structures.
Ask questions to confirm assumptions: Is the cache expected to be thread-safe? What is the expected read/write ratio? How should weight be interpreted (e.g., memory size, cost)? What happens when an entry's weight is updated? Confirm that eviction is strictly LRU and that a single overweight entry is rejected.
Propose using a hash map (dictionary) for O(1) key lookup and a doubly linked list to maintain access order. Each node stores key, value, weight, and pointers to prev/next. Maintain a running total weight.
For get(key): if key exists, move node to the front of the list (most recently used) and return value. For put(key, value, weight): if weight > maxWeight, reject. If key exists, update value and weight, adjust total weight, and move to front. If new key, check if total weight + weight > maxWeight; if so, evict from the tail (least recently used) until enough space, then insert new node at front. Update total weight accordingly.
Discuss handling of zero or negative weights (if allowed), updating weight of existing key, and thread safety. For concurrency, suggest using a mutex or read-write lock, or sharding the cache to reduce contention.
State that both get and put are O(1) time complexity. Discuss trade-offs: memory overhead of linked list nodes, potential for frequent evictions if weights vary widely, and alternative eviction policies (e.g., LFU) if access patterns are skewed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.