Workspace Design Twitter
0d
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 STUDIO
LEARN BY DOING

Predict. Execute. Understand.

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

1 / 7Execution step
Reference example · JavaScript
Inspect reference input
[
  [
    "post",
    1,
    5
  ],
  [
    "post",
    2,
    6
  ],
  [
    "follow",
    1,
    2
  ],
  [
    "feed",
    1
  ],
  [
    "unfollow",
    1,
    2
  ],
  [
    "feed",
    1
  ]
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
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
};

Execution canvas

Active Changed
STEP 1

Execute post

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

Tweets1 rows
Follows0 items

Empty at this step.

Feeds0 items

Empty at this step.

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

[
  [
    "post",
    1,
    5
  ],
  [
    "post",
    2,
    6
  ],
  [
    "follow",
    1,
    2
  ],
  [
    "feed",
    1
  ],
  [
    "unfollow",
    1,
    2
  ],
  [
    "feed",
    1
  ]
]

Execution steps

  1. Execute post
  2. Execute follow
  3. Execute feed
  4. 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.