← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Amazon SWE interview, got hit with the classic LRU cache design question. Nothing too surprising if you've prepped, but the O(1) constraint is where people slip up.

Questions Asked (1)

Q1

Design a data structure for an LRU cache that supports get and put operations, both in O(1) time. The cache should evict the least recently used item when it exceeds capacity.

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

The O(1) requirement is the whole point of the question.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements and constraints, then propose a combination of a hash map and a doubly linked list to achieve O(1) operations. Explain how the hash map provides fast access to nodes, while the doubly linked list maintains the order of usage for eviction. Walk through the get and put operations, highlighting edge cases like updating existing keys and evicting the LRU item.

Pro tip: Mention that you would use a doubly linked list with sentinel head and tail nodes to simplify edge cases and avoid null checks, and discuss how this design can be extended for thread safety if needed.

1. Clarify Requirements and Constraints

Ask about expected cache size, concurrency requirements, and whether keys/values are generic. Confirm that both get and put must be O(1) and that eviction policy is strictly LRU.

2. Propose Data Structures

Suggest using a hash map (dictionary) for O(1) key lookup and a doubly linked list to track usage order. Explain that the hash map stores key -> node references, and the linked list maintains most-recently-used at one end and least-recently-used at the other.

3. Detail Operations

Describe get: if key exists, move its node to the front (most recently used) and return value; else return -1. Describe put: if key exists, update value and move to front; else create new node, add to front, and if capacity exceeded, remove node at tail (LRU) and delete its key from hash map.

4. Handle Edge Cases and Optimizations

Discuss handling capacity 0 or 1, updating existing keys, and using sentinel nodes to simplify list operations. Mention potential thread-safety considerations if the cache will be accessed concurrently.

5. Analyze Complexity and Trade-offs

Confirm that both operations are O(1) time and O(capacity) space. Discuss trade-offs: this design uses extra space for pointers and hash map overhead, but it's optimal for time. Compare with alternatives like using an ordered dictionary (if available) or a combination of hash map and min-heap (which would be O(log n)).

Key Points to Mention

  • Hash map provides O(1) access to cache nodes by key.
  • Doubly linked list maintains usage order with O(1) insertion and deletion.
  • Sentinel nodes (dummy head and tail) simplify edge cases.
  • Eviction removes the tail node (least recently used) and its key from the hash map.
  • Both get and put operations are O(1) time complexity.
  • Space complexity is O(capacity) due to storage of nodes and hash map entries.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.