Classic LRU with a twist: instead of counting slots, you're tracking total weight and evicting until you're back under budget.
Clarify that the cache capacity is defined by total quantity, not item count, and that each item has a weight (quantity). Then design a data structure combining a hash map for O(1) access with a doubly linked list for O(1) recency updates, where eviction removes least recently used items until total weight is within capacity. Explain how to handle updates to an item's quantity, including adjusting total weight and potentially evicting multiple items.
Pro tip: Discuss the trade-off between strict O(1) and the possibility of evicting multiple items on a single put; emphasize that amortized O(1) is acceptable and that the design should handle edge cases like an item larger than capacity.
Confirm that capacity is a total quantity limit, each item has a weight (quantity), and that get/put must be O(1) average. Ask about handling items with weight > capacity and whether quantity can be updated.
Use a hash map for O(1) key lookup, mapping to nodes in a doubly linked list that maintains recency order (most recently used at head). Track total weight separately.
For get: if key exists, move node to head and return value. For put: if key exists, update value and weight, adjust total weight, move to head, then evict from tail while total weight > capacity. If new, insert at head, add weight, then evict.
Evict least recently used items from tail until total weight <= capacity. If a single item's weight exceeds capacity, decide whether to reject it or evict everything and still not store it. Ensure total weight is updated correctly on updates and removals.
Explain that each operation is O(1) average, but eviction may remove multiple items, making put amortized O(1) in terms of items evicted. Discuss memory overhead and potential for starvation of large items.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.