Workspace Lfu Cache
0d
Back to questionsDSA Visualizers: 388 Coding Questions

LFU Cache visualizer

Learn LFU Cache with a step-by-step Stacks & Queues visualizer, JavaScript source code and a worked example. Practice monotonic structures.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 9Execution step
Reference example · JavaScript
Inspect reference input
{
  "capacity": 2,
  "ops": [
    [
      "put",
      1,
      1
    ],
    [
      "put",
      2,
      2
    ],
    [
      "get",
      1
    ],
    [
      "put",
      3,
      3
    ],
    [
      "get",
      2
    ],
    [
      "get",
      3
    ],
    [
      "put",
      4,
      4
    ],
    [
      "get",
      4
    ]
  ],
  "lfu": true
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
capacity,ops,lfu
},s)=>{
let m=new Map,o=[],t=0;
for(let [op,k,v] of ops){
t++;
if(op==="get"){
let x=m.get(k);
o.push(x?x.v:-1);
if(x){
x.f++;
x.t=t
}
}else if(m.has(k)){
let x=m.get(k);
x.v=v;
x.f++;
x.t=t
}else{
if(m.size===capacity){
let q=[...m].sort((a,b)=>(lfu?a[1].f-b[1].f:0)||a[1].t-b[1].t)[0];
m.delete(q[0])
}
m.set(k,{
v,f:1,t
})
}
s("Execute "+op+" "+k,{
Cache:[...m].map(([k,x])=>k+":"+x.v+" f="+x.f+" t="+x.t),Output:o
})
}
return o
};

Execution canvas

Active Changed
STEP 1

Execute put 1

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Cache1 items
Output0 items

Empty at this step.

Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

Topic: Stacks & Queues · Monotonic structures

Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.

Example input

{
  "capacity": 2,
  "ops": [
    [
      "put",
      1,
      1
    ],
    [
      "put",
      2,
      2
    ],
    [
      "get",
      1
    ],
    [
      "put",
      3,
      3
    ],
    [
      "get",
      2
    ],
    [
      "get",
      3
    ],
    [
      "put",
      4,
      4
    ],
    [
      "get",
      4
    ]
  ],
  "lfu": true
}

Execution steps

  1. Execute put 1
  2. Execute put 2
  3. Execute get 1
  4. Execute put 3
  5. Execute get 2
  6. Execute get 3
  7. Execute put 4
  8. Execute get 4

Computed output

[
  1,
  -1,
  3,
  4
]
JavaScript reference implementation
const solve = ({
  capacity,ops,lfu
},s)=>{
  let m=new Map,o=[],t=0;
  for(let [op,k,v] of ops){
    t++;
    if(op==="get"){
      let x=m.get(k);
      o.push(x?x.v:-1);
      if(x){
        x.f++;
        x.t=t
      }
    }else if(m.has(k)){
      let x=m.get(k);
      x.v=v;
      x.f++;
      x.t=t
    }else{
      if(m.size===capacity){
        let q=[...m].sort((a,b)=>(lfu?a[1].f-b[1].f:0)||a[1].t-b[1].t)[0];
        m.delete(q[0])
      }
      m.set(k,{
        v,f:1,t
      })
    }
    s("Execute "+op+" "+k,{
      Cache:[...m].map(([k,x])=>k+":"+x.v+" f="+x.f+" t="+x.t),Output:o
    })
  }
  return o
};

Continue learning

Practice related DSA questions in the same topic.