Build an LRU Cache for Hot Profile Lookups
Last updated: November 29, 2026
Quick Overview
Implement a fixed-capacity cache with get(key) and put(key, value), both in O(1) time. When the cache is full, put evicts the least recently used entry, and both get and put count as a use.
Meta
November 29, 20260
0
0 solved
Implement a fixed-capacity cache with get(key) and put(key, value), both in O(1) time. When the cache is full, put evicts the least recently used entry, and both get and put count as a use.
Caching hot profile and graph data is central to how Meta serves reads, so the LRU cache appears often in coding rounds. The interviewer expects a hash map plus doubly linked list built by hand, clean pointer updates, and a discussion of thread safety and eviction alternatives.
What the Interviewer Expects
- Combines a hash map with a doubly linked list
- Uses sentinel head and tail nodes to remove edge cases
- Moves entries to the front on both get and put
- Evicts from the tail and deletes from the map
- Discusses concurrency and alternatives like LFU
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Make it safe for concurrent access from many threads.
- Add a time-to-live per entry.
- Implement LFU eviction with O(1) operations.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Capacity is a positive integer. Keys and values are integers; get returns -1 on a miss. Updating an existing key changes its value and makes it most recent without growing the size....
High-Level Design
A dict maps key to node, giving O(1) lookup. A doubly linked list orders nodes from most recent (after head) to least recent (before tail). Sentinels mean every real node always has both neighbors...