Data Structures · Visualization

LFU Cache: Introduction

LFU Cache

An LFU (Least Frequently Used) cache stores a limited number of items and, when full, evicts whichever item has been accessed the fewest number of times — rather than the one used longest ago (which is what LRU does). Time: O(1) for both get and put, with a well-designed frequency-bucket structure. Space: O(capacity).

Big-O: get / put both O(1).

LFU Cache: Introduction

LFU Cache

An LFU (Least Frequently Used) cache stores a limited number of items and, when full, evicts whichever item has been accessed the fewest number of times — rather than the one used longest ago (which is what LRU does). If there's a tie in frequency, it's common to fall back to evicting the least recently used among the tied items. It's typically implemented using a hash table for lookups plus a structure that groups items by their access frequency.

How LFU Cache: Introduction Works

  1. Set a fixed capacity for how many items the cache can hold.
  2. Track a usage count (frequency) for every item, starting at 1 when first added.
  3. Every time an item is accessed, increase its frequency count.
  4. When the cache is full and a new item must be added, evict the item with the lowest frequency count (breaking ties by least recently used).

LFU Cache: Introduction Worked Example

Input: Capacity 2. Put(1,A), Put(2,B), Get(1), Get(1), Put(3,C)

  1. Put(1,A) → freq(1)=1
  2. Put(2,B) → freq(1)=1, freq(2)=1
  3. Get(1) → freq(1)=2
  4. Get(1) → freq(1)=3
  5. Put(3,C) → cache full, evict lowest frequency: item 2 (freq=1)

Output: Final cache: {1:A (freq 3), 3:C (freq 1)} (2 was evicted)

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
LRU — Introduction
Evicts the least-recently-used entry when the cache is full. A hash map gives O(1) lookups, while a doubly linked list tracks recency order so the oldest entry is always easy to find.
Empty cache — capacity 5
Active Value
LRU — Introduction
Press ▶ Run to watch it step by step.
Python · LRU & LFU Caches in Code
from collections import OrderedDict

# Hash map for O(1) lookups + an order
# structure to decide WHAT to evict.
# example: intro key=1 value=A

LFU Cache: Introduction Time & Space Complexity

TimeO(1) for both get and put, with a well-designed frequency-bucket structure
SpaceO(capacity)

When to Use LFU Cache: Introduction

  • Caching systems where some items are accessed far more often than others long-term
  • Content delivery networks caching consistently popular content
  • Database buffer pools that want to keep 'hot' data resident

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.