15% off all plans & roadmaps Use code
See plansLeetCode 146 · Solution & editorial
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
LRU Cache is a Design Patterns problem, in the Design (General/Specific) subpattern. The pattern covers tries, LRU/LFU cache, and data structure design
Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.
The reference solution runs in O(1) time and O(capacity) space.
LRU Cache has been reported in interviews at Amazon, Microsoft, Facebook, Apple, Bloomberg, Intuit, Snapchat, eBay.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
LRU Cache is a Design Patterns problem, in the Design (General/Specific) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.