I knew LRU cold but LFU tripped me up at first because you need to track frequency AND recency within the same frequency bucket.
Start by clarifying the requirements: O(1) get and put, and how to handle ties in frequency. Then propose a design using a hash map for key-to-node mapping and a doubly linked list of frequency buckets, each containing a doubly linked list of nodes with that frequency. Explain how get and put update frequencies and evict the least frequently used node in O(1).
Pro tip: Mention that you can optimize by using a min-frequency pointer to avoid scanning for the lowest frequency, and discuss tie-breaking (e.g., LRU among same frequency) to show attention to detail.
Confirm that both get and put must be O(1), and discuss how to handle frequency ties (e.g., evict least recently used among least frequently used).
Use a hash map for O(1) key lookup, and a doubly linked list of frequency nodes, each containing a doubly linked list of cache entries with that frequency.
If key exists, retrieve the node, increment its frequency, and move it to the appropriate frequency list; update min frequency if needed. Return the value.
If key exists, update value and increment frequency. If new, insert with frequency 1; if capacity exceeded, evict the least frequently used node (and LRU among ties) from the min frequency list.
Explain how each operation is O(1) due to constant-time list manipulations and hash map access. Discuss edge cases like capacity 0, updating existing keys, and frequency overflow.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.