BeginnerCoder

Lesson 25 — Ultimate: LRU cache class

Boss level: build a cache

Implement least-recently-used eviction with an OrderedDict — a real systems-design task. move_to_end marks a key as recent; popitem(last=False) removes the oldest.

Why it matters: LRU caches sit inside browsers, databases and CPUs.

Want it another way?
Task

Finish put(): when the cache is over capacity, evict the least recently used key. Expected output: -1 1 3

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Python Intermediate

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.