Resource · Guide or article
Least Recently Used (LRU) Cache
Frames LRU cache design as a class with get and set, both in O(1) average time, which is a common object-design exercise in coding interviews. Assumes you already know hash maps and doubly linked lists.
Reviewed
Open the original: Least Recently Used (LRU) Cache (opens the original site in a new tab)Add to a roadmap
Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.
Source content: English
A Least Recently Used (LRU) Cache organizes items in order of use, allowing you to quickly identify which item hasn't been used for the longest amount of time. Picture a clothes rack, where clothes are always hung up on one side. To find the least-recently used item, look at the item on the other end of the rack. The functions get() and set() must each run in O(1) average time complexity.
PrepKit overview
PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.
- Suited to
- The source does not state a level
- Format
- Lesson
What it covers
- Problem statement: capacity, get returning undefined on a miss, and set with eviction of the least recently used key
- Version 1 pairs a hash map with a doubly linked list for lookup, promotion and eviction
- Version 2 uses JavaScript Map insertion order, deleting and re-adding keys to mark recent use
- Complexity table: O(1) average get and set, O(n) space
How to use it
PrepKit study advice, not a claim made by the source.
Implement version 1 without looking, then trace a capacity-2 cache through five operations on paper and check that your evictions match the trace.
Scope and limitations
The Map-based version only works in JavaScript or other languages with insertion-ordered maps. Code and test cases are in the folder's files rather than on the page.
Access and format
README on GitHub in trekhleb/javascript-algorithms (MIT), linking two implementations and their tests; a Korean version is linked. English, free.
Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88
Evidence behind this overview
- src/data-structures/lru-cache/README.md
Pages that include this
Get the PrepKit newsletter
Optional. You don't need it to use PrepKit.