My first instinct was to use OrderedDict and call it done.
First, restate the LRU cache requirements and confirm the API (get/put) and constraints. Then, design the data structure: a hash map for O(1) key lookup and a doubly linked list to maintain recency order, with the most recently used at the head and least recently used at the tail. Walk through the operations step-by-step, handling edge cases like updating an existing key and evicting when capacity is exceeded.
Pro tip: Mention that you would use dummy head and tail nodes to simplify insertion and deletion logic, avoiding null checks and making the code cleaner and less error-prone.
Confirm the operations (get, put), capacity behavior, and expected time complexity (O(1) for both). Ask about thread safety if relevant.
Explain that a hash map provides O(1) access to nodes, and a doubly linked list maintains usage order. Together they enable O(1) get and put.
Describe the node with key, value, prev, and next pointers. The cache holds a map and pointers to head (most recent) and tail (least recent), possibly using dummy nodes.
For get: if key exists, move node to head and return value; else return -1. For put: if key exists, update value and move to head; else create node, add to head, and if over capacity, remove tail and delete from map.
Walk through examples: capacity 1, updating existing key, eviction order, and get on missing key. Verify O(1) time and correct recency updates.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.