Classic hashmap plus doubly linked list combo.
Start by clarifying requirements and constraints, then propose a hash map combined with a doubly linked list to achieve O(1) operations. Walk through the design, implement the core methods, and discuss trade-offs and edge cases.
Pro tip: Mention that you would use a doubly linked list with sentinel head and tail nodes to simplify edge cases, and that you would consider thread-safety if the cache is shared across threads.
Ask about capacity constraints, thread-safety, and expected operations. Confirm that get and put must be O(1) and that eviction is based on least recent use.
Propose a hash map for O(1) key lookup and a doubly linked list to track usage order. Explain that the map stores key to node pointers, and the list maintains most-recently used at one end and least-recently used at the other.
Detail how get moves the accessed node to the front (most recently used) and returns its value. For put, insert or update the node, move it to the front, and if capacity is exceeded, remove the tail node and delete its key from the map.
Use dummy head and tail nodes to avoid null checks and simplify insertion/removal. Write clean code for addToFront, removeNode, and moveToFront helper methods.
State that both operations are O(1) time and O(capacity) space. Discuss edge cases like capacity 0 or 1, updating existing keys, and concurrency if needed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.