Workspace Task Scheduler
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Task Scheduler visualizer

Learn Task Scheduler with a step-by-step Heaps visualizer, JavaScript source code and a worked example. Practice priority queues and top k.

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
{
  "tasks": [
    "A",
    "A",
    "A",
    "B",
    "B",
    "B"
  ],
  "n": 2
}

Algorithm source

JavaScript reference implementation · highlighted trace statement
const solve = ({
tasks,n
},s)=>{
let m=new Map;
for(let x of tasks)m.set(x,(m.get(x)||0)+1);
let ready=new Map,slots=[],t=0;
while([...m.values()].some(x=>x>0)){
let a=[...m].filter(([x,c])=>c>0&&(ready.get(x)||0)<=t).sort((a,b)=>b[1]-a[1]);
if(a.length){
let x=a[0][0];
slots.push(x);
m.set(x,m.get(x)-1);
ready.set(x,t+n+1)
}else slots.push("idle");
t++;
s("Schedule eligible highest-frequency task",{
Timeline:slots,Remaining:[...m].map(p=>p.join(":"))
})
}
return t
};

Execution canvas

Active Changed
STEP 1

Schedule eligible highest-frequency task

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

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

Worked example and algorithm walkthrough

Topic: Heaps · Priority queues and top K

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

{
  "tasks": [
    "A",
    "A",
    "A",
    "B",
    "B",
    "B"
  ],
  "n": 2
}

Execution steps

  1. Schedule eligible highest-frequency task

Computed output

8
JavaScript reference implementation
const solve = ({
  tasks,n
},s)=>{
  let m=new Map;
  for(let x of tasks)m.set(x,(m.get(x)||0)+1);
  let ready=new Map,slots=[],t=0;
  while([...m.values()].some(x=>x>0)){
    let a=[...m].filter(([x,c])=>c>0&&(ready.get(x)||0)<=t).sort((a,b)=>b[1]-a[1]);
    if(a.length){
      let x=a[0][0];
      slots.push(x);
      m.set(x,m.get(x)-1);
      ready.set(x,t+n+1)
    }else slots.push("idle");
    t++;
    s("Schedule eligible highest-frequency task",{
      Timeline:slots,Remaining:[...m].map(p=>p.join(":"))
    })
  }
  return t
};

Continue learning

Practice related DSA questions in the same topic.