oneby7

Heaps Simulating with a heap

416. Design Twitter

Medium

Design a small version of Twitter. Users post tweets, follow and unfollow each other, and read a news feed of recent tweets. Write a class Twitter with a constructor and four methods:

  • Twitter() creates an empty service, with no tweets and no follows.
  • postTweet(userId, tweetId) posts a new tweet with the id tweetId by the user userId.
  • getNewsFeed(userId) returns the ids of the 10 most recent tweets in the user's news feed, most recent first. The news feed holds the user's own tweets and the tweets of everyone the user follows at the time of the call. If the feed holds fewer than 10 tweets, return all of them.
  • follow(followerId, followeeId) makes the user followerId follow the user followeeId.
  • unfollow(followerId, followeeId) makes the user followerId stop following the user followeeId.

A tweet is more recent than another when it was posted later. Tweet ids are only labels: a larger id does not mean a newer tweet. Your own tweets are always in your feed. Following someone you already follow, following yourself, unfollowing yourself and unfollowing someone you do not follow change nothing.

Your class is tested by replaying a list of calls on a new Twitter. ops[i] names the i-th call, and users[i] is its first argument: the userId of postTweet and getNewsFeed, and the followerId of follow and unfollow. The postTweet calls take their tweet ids from tweetIds in order. The follow and unfollow calls take their followeeId from followees in order. The answer is the list of every feed that getNewsFeed returns, in the order of the calls.

In C# and Go the methods are PostTweet, GetNewsFeed, Follow and Unfollow, and in Rust they are post_tweet, get_news_feed, follow and unfollow. Go creates the service with Constructor(), and Rust with Twitter::new(). The names in ops are the same in every language.

Example 1

Input: ops = ["postTweet", "postTweet", "postTweet", "getNewsFeed", "follow", "getNewsFeed", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed", "getNewsFeed"] users = [1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 2] tweetIds = [40, 13, 7, 99] followees = [2, 2]

Output: [[7,40],[7,13,40],[99,7,13,40],[7,40],[99,13]]

Explanation: User 1 posted 40 and then 7, so 7 comes first. After follow(1, 2), tweet 13 joins the feed, even though it was posted before the follow. After the unfollow, user 2's tweets leave user 1's feed, but they stay in user 2's own feed.

Example 2

Input: ops = ["postTweet", "postTweet", "postTweet", "postTweet", "postTweet", "postTweet", "postTweet", "postTweet", "postTweet", "postTweet", "postTweet", "postTweet", "follow", "getNewsFeed", "getNewsFeed"] users = [5, 9, 5, 9, 5, 9, 5, 9, 5, 9, 5, 9, 5, 5, 9] tweetIds = [3, 8, 15, 4, 23, 42, 16, 0, 1000000000, 77, 61, 5] followees = [9]

Output: [[5,61,77,1000000000,0,16,42,23,4,15], [5,77,0,42,4,8]]

Explanation: User 5's feed holds all twelve tweets, so it returns the 10 most recent and leaves out 3 and 8, the two oldest. User 9 does not follow user 5, so user 9's feed holds only their own six tweets.

Example 3

Input: ops = ["getNewsFeed", "follow", "postTweet", "follow", "unfollow", "unfollow", "getNewsFeed", "follow", "follow", "getNewsFeed"] users = [4, 4, 4, 4, 4, 4, 4, 6, 6, 6] tweetIds = [0] followees = [4, 6, 8, 4, 4, 4]

Output: [[],[0],[0]]

Explanation: The first feed is empty. Following yourself, unfollowing a user you never followed and unfollowing yourself all change nothing, so tweet 0 stays in user 4's feed. User 6 follows user 4 twice, which is the same as once, so tweet 0 appears once.

Check your understanding

Work out the answer for this input before you write any code.

ops = ["postTweet", "postTweet", "postTweet", "follow", "getNewsFeed"] users = [1, 2, 1, 2, 2] tweetIds = [30, 10, 20] followees = [1]

Constraints

  • 1 ≤ ops.length ≤ 2 × 105
  • Each ops[i] is "postTweet", "getNewsFeed", "follow" or "unfollow".
  • users.length equals ops.length.
  • tweetIds.length equals the number of "postTweet" calls in ops.
  • followees.length equals the number of "follow" and "unfollow" calls in ops.
  • 1 ≤ users[i], followees[i] ≤ 105
  • 0 ≤ tweetIds[i] ≤ 109
  • All tweet ids are distinct.
  • A user follows at most 100 other users at any time.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.