DSA
    intermediate

    LRU Cache Design

    LRU Cache Design

    A classic design problem combining HashMap for O(1) lookup with Doubly Linked List for O(1) ordering. Foundation for caching systems and system design interviews.

    ⏱ O(1)
    💾 O(capacity)
    Ready
    capacity=3 | size=0
    Doubly Linked List — HEAD (most recent) → TAIL (evict next)
    HEAD
    empty
    ↔
    TAIL
    HashMap — O(1) lookup: key → node position
    empty
    accessed/inserted evicted cache miss

    Initialize LRU Cache with capacity 3. HashMap for O(1) lookup, DLL for O(1) order.

    50%
    Live Variables
    capacity3
    size0

    Who does which job?

    Sort

    Sort each responsibility into the structure that handles it.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    Hash map

    Drop items here

    Doubly linked list

    Drop items here
    6 pieces left to place

    Test your knowledge

    20 questions
    Question 1 of 20
    Score: 0

    Why use a Doubly Linked List instead of Singly Linked List?

    Write the code

    Runs your solution

    LRU Cache

    Design a data structure that follows LRU (Least Recently Used) cache. Implement get(key) and put(key, value) in O(1) time complexity.

    Example Cases

    Input:[["LRUCache","put","put","get","put","get","put","get","get","get"],[[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]]]
    Output:[null,null,null,1,null,-1,null,-1,3,4]
    javascript

    The short version

    Think of a small desk that fits only a fixed number of papers. Every time you touch a paper, it moves to the front of the stack. When a new paper arrives and the desk is full, whichever paper has sat untouched the longest — at the back — gets bumped off. The challenge: make both 'touch' and 'bump off' instant (O(1)).

    In more depth

    HashMap + Doubly Linked List

    The key insight: we need two data structures working together.

    HashMap: key → Node pointer (O(1) lookup)

    DLL: Maintains order from most recent (head) to least recent (tail)

    Operations:

    get(key): If found, move node to head, return value
    put(key, val): If exists, update and move to head. If new and capacity exceeded, remove tail, then add to head.

    Sentinel Nodes:

    Use dummy head and tail nodes to eliminate edge case checks:

    HEAD <-> Node1 <-> Node2 <-> TAIL

    Key Gotchas:

    Always move accessed node to front (even on get!)
    Remove from both HashMap AND DLL when evicting
    Capacity 0 is a valid edge case

    When to reach for this

    • Cache with size limit and eviction policy
    • Need O(1) for both access and modification
    • Ordering based on recency or frequency
    • System design: caching layers

    Interview Questions

    Amazon
    Google
    Facebook
    Microsoft
    Amazon
    Google
    Twitter
    Amazon
    Uber
    Amazon
    Microsoft
    Facebook

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required