Design Twitter
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-02-11
Problem Statement
Design the backend logic of a Twitter-like service focusing on tweets, users, follow relationships, timelines, and interactions. The focus is on object-oriented design, modularity, and handling core functionalities efficiently.
✅ Functional Requirements
1.User Management
•Users can register, login, and update their profile.
•Each user has a unique ID, username, display name, and profile details.
2.Tweet Management
•Users can post tweets (text-based, optionally with media).
•Users can delete or edit their tweets.
•Tweets have attributes: id, userId, content, timestamp, and optional media.
3.Follow / Unfollow
•Users can follow/unfollow other users.
•Maintain a list of followers and following for each user.
4.Timeline / Feed
•Users can see a feed of tweets from users they follow, ordered by time or relevance.
•Support pagination and limit number of tweets returned.
5.Engagement
•Users can like, retweet, and comment on tweets.
•Maintain count of likes, retweets, and comments per tweet.
6.Optional Features
•Hashtags and mentions.
•Notifications for likes, retweets, mentions.
🔐 Constraints & Assumptions
•Focus on LLD and object-oriented design, not UI.
•System should handle large number of users and tweets efficiently.
•Assume in-memory or database-backed storage for simplicity.
•Ignore advanced features like search ranking or machine learning recommendations.
🧱 Key Components & Classes
1.User Class
1
2class User {
3private:
4 int userId;
5 string username;
6 string displayName;
7 unordered_set<int> followers;
8 unordered_set<int> following;
9public:
10 void follow(User* user);
11 void unfollow(User* user);
12 vector<Tweet*> getTimeline();
13};1.Tweet Class
1
2class Tweet {
3private:
4 int tweetId;
5 int userId;
6 string content;
7 DateTime timestamp;
8 int likes;
9 int retweets;
10 vector<Comment*> comments;
11public:
12 void like();
13 void retweet();
14 void addComment(Comment* comment);
15};1.Comment Class
1
2class Comment {
3private:
4 int commentId;
5 int userId;
6 string content;
7 DateTime timestamp;
8public:
9 // Comment-related methods
10};1.Timeline / Feed Class
1
2class Timeline {
3public:
4 vector<Tweet*> getFeed(User* user, int limit, int offset);
5};1.Optional NotificationService
•Sends notifications for likes, retweets, and mentions.
🔄 Example Workflow
1.User A posts a tweet → tweet saved in storage with timestamp.
2.User B follows User A → User B’s timeline now includes User A’s tweets.
3.User C likes or retweets User A’s tweet → tweet counters updated.
4.Timeline fetch → fetch latest tweets from all followed users, ordered by time.
🧪 Expected Deliverables
•Class diagram showing User, Tweet, Comment, Timeline, and optional NotificationService.
•Implementation of tweet posting, following/unfollowing, feed generation, and interactions.
•Test cases:
•Posting, editing, deleting tweets
•Following/unfollowing users
•Fetching timeline for a user
•Likes, retweets, and comments
💡 Discussion Points
•Data structures for efficient follower/following management.
•Feed generation: pull vs push model.
•Handling scalability for millions of users and tweets.
•Extensibility for hashtags, mentions, and notifications.