← Microsoft Interview Insights

Microsoft·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Microsoft SWE interview, got asked to implement an LRU cache. Pretty standard but the details trip you up if you're not careful.

Questions Asked (1)

Q1

Implement an LRU (Least Recently Used) cache with get and put operations, both in O(1) time.

Algorithms & Data StructuresSystem Design
Author's notes

I knew the answer going in but still fumbled the doubly linked list pointer updates under pressure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Combine a hash map for O(1) key lookup with a doubly linked list to track usage order, where the most recently used item is at the head and the least recently used at the tail. On get, move the accessed node to the head; on put, insert new nodes at the head and evict the tail when capacity is exceeded.

Pro tip: Clarify the cache's concurrency requirements upfront—if it needs to be thread-safe, mention that a single lock or striped locking would be needed, but keep the core design simple unless asked. Also, discuss edge cases like capacity 0 or 1 and updating an existing key.

1. Clarify requirements and constraints

Ask about expected operations, capacity limits, concurrency needs, and whether keys/values are generic. Confirm that both get and put must be O(1) and that eviction happens when capacity is exceeded.

2. Choose data structures

Select a hash map for O(1) access to nodes and a doubly linked list to maintain usage order. Explain that the hash map stores key -> node, and the list allows O(1) removal and insertion.

3. Define node and list operations

Describe the node structure (key, value, prev, next) and helper methods to add a node to the head, remove a node, and move a node to the head. These operations are the building blocks for get and put.

4. Implement get and put

For get: if key exists, move its node to the head and return value; else return -1. For put: if key exists, update value and move to head; else create a new node, add to head, and if capacity exceeded, remove the tail node and delete its key from the map.

5. Analyze complexity and edge cases

Confirm that both operations are O(1) time and O(capacity) space. Discuss edge cases: capacity 0, capacity 1, updating an existing key, and handling null values if applicable.

Key Points to Mention

  • Hash map provides O(1) key lookup, doubly linked list provides O(1) insertion/removal for recency ordering.
  • Most recently used (MRU) at head, least recently used (LRU) at tail; eviction removes tail.
  • On get, move accessed node to head; on put, insert new node at head and evict tail if over capacity.
  • Use dummy head and tail nodes to simplify edge cases in list manipulation.
  • Time complexity: O(1) for both get and put; space complexity: O(capacity).
  • Consider thread-safety if required, but clarify with interviewer before adding complexity.

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