LRU Cache

Medium · Design · Hash Table · Linked List · Design

Problem

Build a fixed-capacity cache that evicts whatever was used least recently. Both operations must run in constant time.

Tests replay a call sequence: the first entry names the class and its constructor arguments, each later entry is a method with its arguments, and calls returning nothing report None.

Examples

Input: LRUCache(2), put(1,1), put(2,2), get(1), put(3,3), get(2)
Output: 1, then -1

get(1) refreshes key 1, so adding key 3 evicts key 2 instead.

Constraints

Approaches

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…