Merge Followed Timelines While Collapsing Duplicate Reposts

Last updated: November 29, 2026

Quick Overview

Given each followed account's posts sorted newest first, where a post may be a repost of another post, return the k newest items for the Following timeline so that any piece of content (an original or reposts of it) appears only once, at its newest occurrence.

X
Coding & Algorithms
Software Engineer
X
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given each followed account's posts sorted newest first, where a post may be a repost of another post, return the k newest items for the Following timeline so that any piece of content (an original or reposts of it) appears only once, at its newest occurrence.

The Following tab on X is a reverse-chronological merge of many accounts, and when several people you follow repost the same viral post you should not see it five times. The onsite checks a k-way merge with a heap, the dedupe key choice, and pagination thinking.

What the Interviewer Expects
  • Uses a heap of size at most the number of accounts
  • Dedupes on the original content id, not the repost id
  • Keeps the newest occurrence and explains why
  • Stops after k results without draining every list
  • Discusses cursors for the next page
Key Topics to Cover
k-way merge
heap
deduplication
pagination cursors
timeline
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How do you return the next page without re-reading from the top?
  • Instead of showing the newest repost, show the original with 'reposted by A, B and C'. What changes?
  • Some followed accounts have 50,000 posts per day. How do you bound work?
  • What if the original was deleted but reposts remain?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: a list of per-account lists, each sorted by timestamp descending, with posts as (ts, post_id, author, original_id) where original_id is None for originals. Output: up to k posts, newest...

High-Level Design

Classic k-way merge. Seed a max-heap (negated timestamps) with the head of each list. Pop the newest item; if its content id has not been emitted, emit it; then push the next item from the same list. ...


Submit Your Answer
Markdown supported

Related Questions