Design Twitter like system

Difficulty: EASYCategory: LLDFrequency: 1Asked On 2024-05-22

Problem Statement

Design a simplified version of Twitter, where users can post tweets, interact with tweets via likes/dislikes, follow other users, and view a personalized news feed. The system should be scalable, efficient, and support basic social and content features.

🎯 Functional Requirements

Implement the following operations:

1. postTweet(String userId, String content)

•Creates a tweet posted by the user.
•Each tweet should have a unique ID and a timestamp.
•The tweet is added to the user’s personal feed.

2. likeTweet(String userId, String tweetId)

•Records that a user likes a tweet.
•A user can like a tweet only once.
•A like and dislike cannot coexist for the same user and tweet.

3. dislikeTweet(String userId, String tweetId)

•Records a dislike.
•A user can dislike a tweet only once.
•Cancels any previous like.

4. getTopLikedTweets(String userId, int k)

•Returns top-k tweets posted by the given user, sorted by number of likes (descending).
•If there's a tie, use timestamp or tweet ID to break it.

5. follow(String followerId, String followeeId)

•User with followerId starts following user with followeeId.

6. unfollow(String followerId, String followeeId)

•User with followerId stops following the user.

7. getNewsFeed(String userId, int k)

•Returns the most recent k tweets for a user.
•Includes tweets from:
•The user
•All users they follow
•Sorted in reverse chronological order (latest first).


Solution

Comments