AlgoMaster Logo

Design Twitter

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Approaches

1. Naive Approach

Intuition:

This naive approach involves maintaining three main data structures:

  1. Map of Tweets: A HashMap to store all tweets for each user.
  2. Follow Graph: A HashMap to store follow relationships.
  3. Global Tweet List: A list of all tweets in the order they were posted.

Using these data structures, we can retrieve the latest tweets for any user by filtering the global tweet list according to the follow relationships.

Code:

2. Optimized Approach using Min-Heap

Intuition:

To improve the retrieval of the latest tweets for a user, we utilize a Min-Heap to efficiently manage the fetching of the top 10 most recent tweets. We maintain similar data structures for storing tweets and follow relationships, but enhance the retrieval by using the current timestamp to prioritize recent tweets.

Code: