Algorithms and Data Structures · Middle · Question 27 of 75

How is an LRU cache with O(1) get and put usually built?

Explanation

The map finds a node by key; the list moves it to the front in O(1) and drops the tail when full. In JavaScript a Map alone works: it keeps insertion order, so delete and re-set a key on access and evict map.keys().next().value.

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 27

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz