Design a Twitter-like Microblogging Platform
Difficulty: EASYCategory: LLDFrequency: 1Asked On 2024-05-22
Problem Statement
Designing and implementing an in-memory microblogging system similar to Twitter, supporting core functionalities around tweeting, engagement, following users, and building a personalized feed.
🎯 Functional Requirements
Design and implement the following features:
1. postTweet(userId, tweetId, content)
•A user can post a tweet.
•Each tweet is uniquely identified by tweetId and associated with a timestamp.
•Tweets are stored per user and globally retrievable.
2. likeTweet(userId, tweetId)
•A user can like a tweet.
•A user cannot like the same tweet more than once.
•A user cannot both like and dislike the same tweet.
3. dislikeTweet(userId, tweetId)
•A user can dislike a tweet.
•A user cannot dislike the same tweet more than once.
•A user cannot both like and dislike the same tweet.
4. getTopLikedTweets(userId, k)
•Returns the top k tweets posted by a user, sorted by the number of likes.
•If two tweets have the same number of likes, the most recent one comes first.
5. follow(followerId, followeeId)
•Enables a user (followerId) to follow another user (followeeId).
•A user cannot follow themselves.
•A user cannot follow the same person multiple times.
6. unfollow(followerId, followeeId)
•Enables a user to unfollow a previously followed user.
7. getNewsFeed(userId, k)
•Returns the k most recent tweets from the user and users they follow.
•Tweets should be sorted by most recent timestamp (reverse chronological order).
🧱 Suggested Data Structures
Tweet
1java
2CopyEdit
3class Tweet {
4 int tweetId;
5 int userId;
6 String content;
7 long timestamp;
8 Set<Integer> likes;
9 Set<Integer> dislikes;
10}TwitterService
1java
2CopyEdit
3class TwitterService {
4 Map<Integer, Set<Integer>> followersMap;
5 Map<Integer, List<Tweet>> userTweetsMap;
6 Map<Integer, Tweet> tweetMap;
7 AtomicLong timestampGenerator;
8
9 void postTweet(int userId, int tweetId, String content)
10 void likeTweet(int userId, int tweetId)
11 void dislikeTweet(int userId, int tweetId)
12 List<Tweet> getTopLikedTweets(int userId, int k)
13 void follow(int followerId, int followeeId)
14 void unfollow(int followerId, int followeeId)
15 List<Tweet> getNewsFeed(int userId, int k)
16}