A heap cannot edit the middle
transcript
A feed app shows the trending topics. Counts rise as new posts arrive. Counts fall as old posts leave the window.
Counts change all the time. Their entries sit inside a heap. A heap cannot edit the middle.
When a count changes, push a new entry. Leave the old entry where it is. The heap now holds both.
At the top, compare with the live count. If they differ, skip the entry.
Two topics can share a count. The smaller topic id comes first. Put the id in the entry, to break ties.
A post can be exactly one window old. By then, the post has left. So compare with a strict sign.
In the interview, say what can go stale. Say how you will skip it. And say what stays live.
Each post pushes one or two entries. Each entry is popped at most once. So every refresh stays fast.
Never edit the middle. Verify at the top. Full breakdown in the lecture.