I spent the first few minutes confused about why weight mattered at all.
Start by clarifying requirements: fixed total weight capacity, per-entry weight, LRU eviction, and O(1) average get/put. Then propose a hash map for O(1) access combined with a doubly linked list to maintain LRU order, and explain how to track total weight and evict from the tail when capacity is exceeded. Finally, discuss edge cases and trade-offs.
Pro tip: Mention that you would handle entries heavier than the total capacity by rejecting them, and that you'd consider thread-safety if the cache is shared, showing production awareness.
Confirm that capacity is total weight, each entry has a weight, eviction is LRU, and get/put must be O(1) average. Ask about concurrency, weight updates, and behavior for oversized entries.
Use a hash map for O(1) key lookup and a doubly linked list to maintain access order. Store weight and value in each node, and keep a running total weight.
For get: if key exists, move node to front (most recently used) and return value. For put: if key exists, update value and weight, adjust total weight, and move to front; if new, create node, add to front, update total weight, and evict from tail while total weight exceeds capacity.
If a new entry's weight exceeds total capacity, reject it. During eviction, remove nodes from the tail until total weight <= capacity. Ensure the hash map and list stay in sync.
Explain that both get and put are O(1) average due to hash map and linked list operations. Discuss trade-offs: memory overhead of pointers, potential need for locking in concurrent scenarios, and alternative eviction policies.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.