← Microsoft Interview Insights

Microsoft·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
May 2026

Summary

Microsoft coding round, pretty much just the LRU cache problem. Classic question but the O(1) constraint is where people trip up if they haven't seen it before.

Questions Asked (1)

Q1

Design and implement an LRU cache with get and put operations, both running in O(1) average time. The cache should evict the least recently used entry when it exceeds capacity.

Algorithms & Data StructuresSystem Design
Author's notes

The question itself is fine, I knew what an LRU cache was.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying requirements and edge cases, then propose a hash map combined with a doubly linked list to achieve O(1) operations. Explain how the hash map provides direct access to nodes, while the linked list maintains usage order for eviction. Walk through the get and put logic, and discuss handling of edge cases like updating existing keys and capacity zero.

Pro tip: Mention that you would use a doubly linked list with dummy head and tail nodes to simplify insertion and removal logic, avoiding null checks. Also, discuss thread-safety considerations if the cache might be accessed concurrently, showing awareness of real-world usage.

1. Clarify Requirements and Constraints

Ask about expected capacity, key/value types, thread-safety needs, and whether the cache should be in-memory or distributed. Confirm that O(1) average time is required for both get and put.

2. Choose Data Structures

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

3. Define 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 a new node, add to front, and if capacity exceeded, remove the tail node and delete its key from the hash map.

4. Handle Edge Cases

Discuss capacity zero (always evict), updating existing keys, and ensuring dummy head/tail nodes simplify list operations. Also mention potential concurrency issues if applicable.

5. Analyze Complexity and Optimizations

Confirm that both operations are O(1) average time due to hash map and linked list. Mention possible optimizations like using a custom node class or arrays for performance, and trade-offs with thread-safety.

Key Points to Mention

  • Hash map provides O(1) access to cache nodes.
  • Doubly linked list maintains usage order for O(1) eviction and updates.
  • Dummy head and tail nodes simplify insertion and removal logic.
  • Get operation moves accessed node to the front (most recently used).
  • Put operation updates existing key or inserts new node, evicting LRU if over capacity.
  • Thread-safety considerations (e.g., locks or concurrent data structures) if needed.

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