DSA
    intermediate

    LRU Cache Design

    Software Engineer path · lesson 49 of 99
    time O(1)
    space O(capacity)

    Syntax: put word n · get word. Numbers from -99 to 99.

    Try:
    Capacity
    recent →0
    HitInserted / updatedEvicting

    Empty cache with room for 3.

    Code (running line highlighted)
    1. 1get(k): if k not in map: return -1
    2. 2 move k to the front (most recent); return its value
    3. 3put(k, v): if k in map: update v, move to front
    4. 4 else: if full: evict the back (least recent)
    5. 5 insert (k, v) at the front
    6. 6# hash map + doubly linked list: every step is O(1)
    Hits
    0 / 2
    Misses
    0 / 1
    Evictions
    0 / 1
    Invariant: The list is ordered by last use: front = most recent, back = next to evict.

    Tap any element in the diagram to inspect its state at this step.

    1 / 10

    Keys: ← → step, Space play/pause. Predict mode pauses before key steps and asks what happens next.

    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
    class LRUCache {
      constructor(capacity) {
        // Your code here
      }
      get(key) {
        // Your code here
      }
      put(key, value) {
        // Your code here
      }
    }

    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