Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

operations =
arguments =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Store each user’s tweets with timestamps. News feed is the 10 most recent tweets among the user and followees.

Approach

  • postTweet: append (time, tweetId) to user list.
  • follow/unfollow: maintain followee set.
  • getNewsFeed: k-way merge of the most recent tweet lists using a max-heap keyed by time. Start with each user’s latest tweet, then push the previous tweet from that same user when popped.

Why it works

The heap always contains the next most recent candidate among all lists, producing the correct top-10 ordering.

Code
Loading...
Complexity
Time: O((F+10) log F) per feed
Space: O(F)
Solution
Updated: 2026-02-23

Idea

Store each user’s tweets with timestamps. News feed is the 10 most recent tweets among the user and followees.

Approach

  • postTweet: append (time, tweetId) to user list.
  • follow/unfollow: maintain followee set.
  • getNewsFeed: k-way merge of the most recent tweet lists using a max-heap keyed by time. Start with each user’s latest tweet, then push the previous tweet from that same user when popped.

Why it works

The heap always contains the next most recent candidate among all lists, producing the correct top-10 ordering.

Code
Loading...
Complexity
Time: O((F+10) log F) per feed
Space: O(F)