← All Patterns

Pattern Recognition Drill

#138

Medium Linked Lists

The Problem

Design a data structure for Least Recently Used cache with O(1) get and put.

What approach would you use?

Think about it before scrolling down.

LRU Cache

Key Signals

Hash Map

OrderedDict or HashMap + doubly linked list. Get moves to end. Put evicts front if over capacity. O(1).

Common Trap

A regular dict doesn't track access order. You need either OrderedDict or a manual DLL + HashMap.

← #137 #139 →