Back to questionsDSA Visualizers: 388 Coding Questions Design Twitter visualizer Learn Design Twitter with a step-by-step Heaps visualizer, JavaScript source code and a worked example. Practice priority queues and top k.
INTERACTIVE EXECUTION STUDIOLEARN BY DOING Predict. Execute. Understand. Follow the source code, inspect live state changes, and predict the next operation before revealing it.
1 / 7 Execution step
Prediction modePanel width Reference example · JavaScript
Inspect reference input [
[
"post",
1,
5
],
[
"post",
2,
6
],
[
"follow",
1,
2
],
[
"feed",
1
],
[
"unfollow",
1,
2
],
[
"feed",
1
]
] Code Visuals
JavaScript reference implementation · highlighted trace statement
1 const solve = (ops,s)=>{
2 let tweets=[],m=new Map,o=[];
3 for (let [op,u,v] of ops){
4 if (op==="post")tweets.push([u,v]);
5 else if (op==="follow"){
6 if (!m.has(u))m.set(u,new Set);
7 m.get(u).add(v)
8 }else if (op==="unfollow")m.get(u)?.delete(v);
9 else o.push(tweets.filter(([id])=>id===u||m.get(u)?.has(id)).slice(-10 ).reverse().map(x=>x[1 ]));
10 s("Execute "+op,{
11 Tweets:tweets,Follows:[...m].map(([k,v])=>[k,...v]),Feeds:o
12 })
13 }
14 return o
15 };
Execution canvas Active ChangedSTEP 1 Execute post Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.
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 [
[
"post",
1,
5
],
[
"post",
2,
6
],
[
"follow",
1,
2
],
[
"feed",
1
],
[
"unfollow",
1,
2
],
[
"feed",
1
]
] Execution steps Execute post Execute follow Execute feed Execute unfollow Computed output [
[
6,
5
],
[
5
]
] JavaScript reference implementation const solve = (ops,s)=>{
let tweets=[],m=new Map,o=[];
for(let [op,u,v] of ops){
if(op==="post")tweets.push([u,v]);
else if(op==="follow"){
if(!m.has(u))m.set(u,new Set);
m.get(u).add(v)
}else if(op==="unfollow")m.get(u)?.delete(v);
else o.push(tweets.filter(([id])=>id===u||m.get(u)?.has(id)).slice(-10).reverse().map(x=>x[1]));
s("Execute "+op,{
Tweets:tweets,Follows:[...m].map(([k,v])=>[k,...v]),Feeds:o
})
}
return o
};Continue learning Practice related DSA questions in the same topic.