355. Design Twitter

Design Twitter (LeetCode 355) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Heap (Priority Queue) Patterns pattern, in the K-way Merge subpattern.

How to solve Design Twitter

Design Twitter is a K-way Merge Using Heap problem, part of Heap (Priority Queue) Patterns. Master K-way merge pattern using min heap. Merge k sorted lists and find kth smallest in matrix.

Work towards the solution in these steps:

  1. Keep each user's tweets in reverse chronological order so the newest tweet from a user is easy to access.
  2. Represent following relationships separately from tweet storage, and treat the requesting user as an implicit source of tweets.
  3. The feed is the top 10 items from several already time-ordered tweet streams.

The Design Twitter solution and editorial walks through the full approach with reference code.

Problem statement

Implement a Twitter class that stores users' tweets and follow relationships. The constructor takes no arguments and initializes an empty service. A tweet is identified by its unique tweetId; tweets are ordered by the time their postTweet calls occur, with later calls being more recent.

postTweet(userId, tweetId) records a tweet for userId and returns nothing. follow(followerId, followeeId) makes followerId follow followeeId, while unfollow(followerId, followeeId) removes that relationship if it exists; both methods return nothing.

getNewsFeed(userId) returns a list containing up to the 10 most recent tweet IDs posted by userId or by any user currently followed by userId, ordered from newest to oldest. If no eligible tweets exist, return an empty list. The method does not change the stored state.

Example 1:

Input:

["Twitter", "postTweet", "postTweet", "getNewsFeed", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"] [[],[7,501],[7,502],[7],[7,12],[12,880],[7],[7,12],[7]]

Output:

[null,null,null,[502,501],null,null,[880,502,501],null,[502,501]]

Explanation: User 7 initially sees their two tweets. After following user 12, user 7 also sees tweet 880; unfollowing user 12 removes it from the feed.

Example 2:

Input:

["Twitter", "follow", "postTweet", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed", "unfollow", "getNewsFeed"] [[],[4,9],[9,31],[4,44],[4],[4,9],[4],[4,9],[22]]

Output:

[null,null,null,null,[44,31],null,[44],null,[]]

Explanation: A relationship created before either tweet is posted still allows user 4 to see user 9's tweet. The second unfollow is a no-op, and user 22 has no eligible tweets.

Example 3:

Input:

["Twitter", "postTweet", "follow", "postTweet", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"] [[],[15,700],[3,15],[3,701],[3,15],[15,702],[3],[3,15],[3]]

Output:

[null,null,null,null,null,null,[702,701,700],null,[701]]

Explanation: User 3 follows user 15, and the repeated follow does not change the relationship. The first feed contains the three tweets in reverse posting order; after the unfollow, only user 3's tweet remains visible.

Constraints:

- The first operation is Twitter, and its argument list is empty. - 1 <= operations.length <= 10^5. - Every operation after the constructor is one of postTweet, getNewsFeed, follow, or unfollow. - userId, followerId, and followeeId are JSON integers. - tweetId is a JSON integer, and every tweetId is unique across all postTweet calls. - followerId != followeeId for every follow and unfollow call. - unfollow may name a relationship that does not currently exist; such a call is valid and has no effect.

Topics and companies

Topics: Design, Heap (Priority Queue), Hash Table, Data Stream.

How to practise Design Twitter on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer.

Open Design Twitter in the code editor, or read the Design Twitter editorial for a worked solution with its approach and complexity analysis.

Where Design Twitter sits in the DSA pattern sheet

Problems related to Design Twitter

Other problems that use the same K-way Merge and Heap (Priority Queue) Patterns techniques:

Design Twitter FAQ

What is the LeetCode number of Design Twitter?

Design Twitter is LeetCode problem 355, rated Medium.

What pattern is used to solve Design Twitter?

Design Twitter is a K-way Merge Using Heap problem. It belongs to the Heap (Priority Queue) Patterns pattern, in the K-way Merge subpattern.

Preparing this workspace.