Started fine with the hashmap plus doubly linked list approach, moved the accessed node to the front on both get and put, returned -1 for missing keys.
Start by clarifying requirements (fixed capacity, O(1) get/put, LRU eviction, thread-safety expectations). Then describe the classic hash map + doubly linked list design, covering recency updates, duplicate keys, miss behavior, and concurrency. Finally, outline testing and trade-offs.
Pro tip: Mention that you would first ask about the expected read/write ratio and whether thread-safety is required, as this shows you think about real-world constraints before jumping into code.
Ask about capacity, expected operations, thread-safety needs, and whether keys/values have any special properties. Confirm that O(1) average time is required for both get and put.
Use a hash map for O(1) key lookup and a doubly linked list to maintain recency order. The map stores key -> node, and the list has most recently used at head and least recently used at tail.
For get: if key exists, move node to head and return value; else return null/-1. For put: if key exists, update value and move to head; else create new node, add to head, and if capacity exceeded, remove tail and delete from map.
Discuss options: use a single lock (simple but may bottleneck), or use concurrent data structures with fine-grained locking (e.g., ConcurrentHashMap + synchronized list operations). Mention trade-offs between simplicity and scalability.
Test basic get/put, eviction order, duplicate key updates, capacity 1, and concurrent access. Use unit tests with assertions and stress tests for thread-safety.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.