LRU Cache
Fixed-capacity cache: get and put in O(1), evicting the least recently used key when full.
Intuition
You need O(1) lookup (hash map) AND O(1) reordering by recency (doubly linked list). Marry them: map key → list node; every touch unlinks the node and reinserts at the front; eviction pops the tail. Neither structure alone can do both jobs.
Approach
Why a doubly linked list
Removing a node from the middle in O(1) needs prev and next pointers — recency order is exactly 'move to front on touch'.
Sentinels kill edge cases
Dummy head and tail mean every real node has real neighbours — no null checks on unlink/insert.
The two ops
get: look up, move node to front. put: update-or-create at front; if over capacity, unlink tail.prev and delete its map entry.
Solution & live demo
Edge cases
Update value and refresh recency — must NOT evict.
Every distinct put evicts the previous key; sentinels keep the list sane.