PythiloLesson 21 of 32 · progress kept in this browser

Evict the least recently used key

A cache keeps a limited number of results. When it is full, a least recently used policy drops the key nobody has touched for the longest time. OrderedDict remembers order and can move a key to the end in constant time.

from collections import OrderedDict
recent = OrderedDict(a=1, b=2)
recent.move_to_end("a")
print(list(recent))   # ['b', 'a']
Your task

Write LRUCache(capacity) with get(key), returning None when missing, and put(key, value). A full cache evicts the least recently used key.