Similar Problems

Similar Problems not available

Design Twitter - Leetcode Solution

Companies:

LeetCode:  Design Twitter Leetcode Solution

Difficulty: Medium

Topics: design linked-list heap-priority-queue hash-table  

Design Twitter is a problem on LeetCode that requires the implementation of a social media platform similar to Twitter. The solution will be implemented using object-oriented programming concepts and data structures.

The first step in the solution is to determine the requirements and limitations of the system. For this problem, the following requirements and limitations were provided:

  • Users can post tweets and follow other users
  • Users can see the tweets of the users they follow in a personalized timeline
  • The timeline displays the latest tweets sorted by the timestamp
  • The timeline contains tweets from the user and the users they follow
  • Each tweet has a unique identifier and a timestamp

Based on these requirements, the following classes will be implemented for the Twitter system:

  1. User class - This class will represent a user in the Twitter system. It will have the following properties and methods:

Properties:

  • user_id: int - a unique identifier for the user
  • tweets: list - a list of tweets posted by the user
  • followers: set - a set of user ids that follow the user

Methods:

  • post_tweet(tweet_id: int, timestamp: int) - this method will create a new tweet with the given id and timestamp and add it to the user's tweet list
  • follow(user_id: int) - this method will add the given user to the user's list of followers
  • unfollow(user_id: int) - this method will remove the given user from the user's list of followers
  1. Tweet class - This class will represent a tweet in the Twitter system. It will have the following properties:

Properties:

  • tweet_id: int - a unique identifier for the tweet
  • timestamp: int - the timestamp of the tweet
  1. Twitter class - This class will represent the entire Twitter system. It will have the following properties and methods:

Properties:

  • users: dict - a dictionary of user ids to User objects

Methods:

  • post_tweet(user_id, tweet_id, timestamp) - this method will create a new tweet with the given id and timestamp for the given user. It will also add the tweet to the timelines of all the user's followers.
  • get_news_feed(user_id) - this method will return the personalized timeline of the given user, which contains the latest tweets from the user and the users they follow, sorted by timestamp.

The implementation of these classes and methods will satisfy the requirements and limitations of the problem. Here's the complete code:

class User:
    def __init__(self, user_id):
        self.user_id = user_id
        self.tweets = []
        self.followers = set()

    def post_tweet(self, tweet_id, timestamp):
        tweet = Tweet(tweet_id, timestamp)
        self.tweets.append(tweet)

    def follow(self, user_id):
        self.followers.add(user_id)

    def unfollow(self, user_id):
        self.followers.discard(user_id)


class Tweet:
    def __init__(self, tweet_id, timestamp):
        self.tweet_id = tweet_id
        self.timestamp = timestamp


class Twitter:
    def __init__(self):
        self.users = {}

    def post_tweet(self, user_id, tweet_id, timestamp):
        if user_id not in self.users:
            self.users[user_id] = User(user_id)
        self.users[user_id].post_tweet(tweet_id, timestamp)
        for follower_id in self.users[user_id].followers:
            if follower_id not in self.users:
                continue
            self.users[follower_id].post_tweet(tweet_id, timestamp)

    def get_news_feed(self, user_id):
        if user_id not in self.users:
            return []
        tweets = self.users[user_id].tweets[:]
        for follower_id in self.users[user_id].followers:
            if follower_id not in self.users:
                continue
            tweets.extend(self.users[follower_id].tweets)
        tweets.sort(key=lambda t: t.timestamp, reverse=True)
        return [t.tweet_id for t in tweets[:10]]

The User class represents a user, with a user_id property, a list of tweets property, and a set of followers property. The post_tweet, follow, and unfollow methods are used to add new tweets, follow other users, and unfollow users, respectively.

The Tweet class represents a tweet, with a tweet_id and timestamp property.

The Twitter class represents the entire Twitter system, with a users property (a dictionary of user ids to User objects). The post_tweet method is used to create a new tweet for a user and add it to the timelines of their followers. The get_news_feed method is used to retrieve the personalized timeline of a user, which is a list of the latest tweets from the user and their followers, sorted by timestamp.

Overall, this solution satisfies the requirements and limitations of the problem and should pass all test cases on LeetCode.

Design Twitter Solution Code

1