← Microsoft Interview Insights

Microsoft·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Microsoft SWE interview with a classic LFU cache design question. The O(1) constraint is what makes it tricky and they clearly wanted to see if you knew the doubly linked list trick.

Questions Asked (1)

Q1

Design and implement an LFU cache with get and put operations that both run in O(1) average time. On capacity, evict the least frequently used key, breaking ties by recency.

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

The O(1) part is where most people stumble.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the O(1) requirement and tie-breaking rule, then propose a design combining a hash map for key-to-node lookup with a frequency-indexed doubly linked list to track usage order. Explain how get and put update frequencies and evict the least frequent, least recently used key in constant time.

Pro tip: Mention that you can implement the frequency lists using a single doubly linked list of nodes where each node represents a frequency bucket, and each bucket contains a doubly linked list of keys—this avoids the need for a separate min-frequency variable and simplifies eviction.

1. Clarify requirements and constraints

Confirm that both get and put must be O(1) average time, and that eviction removes the least frequently used key, with ties broken by least recently used. Ask about cache size limits and concurrency if relevant.

2. Design the data structures

Propose a hash map from key to node for O(1) access. Each node stores key, value, and frequency. Maintain a doubly linked list of frequency buckets, each containing a doubly linked list of nodes with that frequency, ordered by recency.

3. Implement get operation

On get, look up the node in the hash map. If found, increment its frequency, move it to the appropriate frequency bucket (creating a new bucket if needed), and return its value. If not found, return -1.

4. Implement put operation

On put, if key exists, update its value and increment frequency (similar to get). If new, insert with frequency 1. If capacity is exceeded, evict the least frequent, least recently used node from the lowest frequency bucket.

5. Analyze complexity and edge cases

Explain that all operations are O(1) average due to hash map and constant-time list manipulations. Discuss edge cases like capacity 0, updating existing keys, and tie-breaking.

Key Points to Mention

  • Use a hash map for O(1) key lookup and a frequency-indexed doubly linked list to maintain usage order.
  • Each node stores key, value, and frequency; frequency buckets are ordered by frequency and recency.
  • On get/put, increment frequency and move node to the next frequency bucket, updating recency.
  • Eviction removes the least frequent, least recently used node from the lowest frequency bucket.
  • Maintain a min-frequency pointer or use the head of the frequency list to achieve O(1) eviction.
  • Handle edge cases: capacity 0, updating existing keys, and tie-breaking by recency.

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