← Amazon Interview Insights

Amazon·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Amazon SWE coding round, one question: implement an LRU cache. Short and to the point.

Questions Asked (1)

Q1

Implement an LRU (Least Recently Used) cache with the standard get and put operations.

Algorithms & Data Structures
Author's notes

Classic question but the pressure of doing it live always makes the doubly linked list part messier than it looks on paper.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements: capacity, thread-safety, and expected time complexity. Then propose a hash map combined with a doubly linked list to achieve O(1) get and put operations, and walk through the implementation details 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 discuss how to make it thread-safe if needed (e.g., using synchronized or ConcurrentHashMap with a lock).

1. Clarify requirements

Ask about capacity limits, concurrency requirements, and whether the cache should be thread-safe. Confirm that get and put should both be O(1).

2. Choose data structures

Propose a hash map for O(1) key lookup and a doubly linked list to maintain access order. Explain that the map stores key -> node, and the list keeps most recently used at the front.

3. Define operations

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

4. Handle edge cases

Discuss edge cases: capacity 0 or 1, updating existing key, and removing the least recently used node correctly. Mention using sentinel nodes to avoid null checks.

5. Analyze complexity and concurrency

State that both operations are O(1) time and O(capacity) space. If thread-safety is required, discuss using synchronized methods or a concurrent approach with fine-grained locking.

Key Points to Mention

  • Hash map provides O(1) access to nodes.
  • Doubly linked list maintains access order and allows O(1) removal and insertion.
  • Sentinel head and tail nodes simplify edge cases.
  • Both get and put must update the order (move accessed/inserted node to front).
  • Eviction removes the least recently used node (tail) when capacity is exceeded.
  • Thread-safety can be achieved with synchronization or concurrent data structures.

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