Advertisement

Design Twitter - LeetCode 355 Solution

Design Twitter - Complete Solution Guide

Design Twitter is LeetCode problem 355, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and is able to see the 10 most recent tweets in the user's news feed. Implement the Twitter class: Twitter() Initializes your twitter object. void postTweet(int userId, int tweetId) Composes a new tweet with ID tweetId by the user userId . Each call to this function will be made with a unique tweetId . List<Integer> getNewsFeed(int userId) Retrieves the 10 most recent tweet IDs in the user's news fe

Detailed Explanation

The problem asks us to design a simplified Twitter class that allows users to post tweets, follow/unfollow other users, and retrieve the 10 most recent tweets in a user's news feed. The news feed should include tweets from the user themselves and the users they follow, ordered from most recent to least recent. Each tweet has a unique ID. We need to implement `postTweet`, `getNewsFeed`, `follow`, and `unfollow` methods.

Solution Approach

The solution uses a combination of hash tables (maps) and a min-heap to implement the Twitter functionality. Each user has a list of tweets stored with timestamps. The `getNewsFeed` method retrieves tweets from the user and their followees, merging them using a min-heap to get the 10 most recent tweets. The min-heap stores (timestamp, tweetId, userId, nextTweetIndex) tuples to efficiently track the next most recent tweet for each user. Timestamps are negative to make the min-heap behave like a max-heap for recent tweets.

Step-by-Step Algorithm

  1. Step 1: Initialize the Twitter class with a timestamp counter, a map to store user tweets (userId -> list of (timestamp, tweetId)), and a map to store followees (followerId -> set of followeeIds).
  2. Step 2: `postTweet(userId, tweetId)`: Decrement the timestamp counter (making it negative to represent recency), and append the (timestamp, tweetId) tuple to the user's tweet list.
  3. Step 3: `getNewsFeed(userId)`: Create a set of relevant user IDs (the user themselves and their followees).
  4. Step 4: Initialize a min-heap (priority queue) to store the most recent tweet from each relevant user. The heap stores tuples of (timestamp, tweetId, userId, tweetIndex).
  5. Step 5: Iterate through the relevant users. For each user, if they have tweets, add their most recent tweet to the min-heap.
  6. Step 6: Repeatedly extract the smallest (most recent) tuple from the heap (up to 10 tweets). Add the tweetId to the news feed list.
  7. Step 7: If the user who posted the extracted tweet has more tweets, add their next most recent tweet to the heap.
  8. Step 8: Return the news feed list.
  9. Step 9: `follow(followerId, followeeId)`: Add the followeeId to the followerId's followee set, after validating that the user is not trying to follow themselves.
  10. Step 10: `unfollow(followerId, followeeId)`: Remove the followeeId from the followerId's followee set.

Key Insights

  • Insight 1: We need to maintain the order of tweets to display the most recent ones first.
  • Insight 2: Using a min-heap (priority queue) to merge the sorted lists of tweets from followed users is efficient for getting the top 10 most recent tweets.
  • Insight 3: We should avoid users following themselves, even though the problem statement constraints guarantee that condition will not occur, it's good practice.

Complexity Analysis

Time Complexity: O(f*log(u))

Space Complexity: O(u+t)

Topics

This problem involves: Hash Table, Linked List, Design, Heap (Priority Queue).

Companies

Asked at: Coupang, PayPal, X.