Tutorialspoint
Problem
Solution
Submissions

Design a Twitter

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 20

Write a C++ program to design a Twitter-like timeline system that supports posting tweets, retrieving news feeds, following and unfollowing users.

Example 1
  • Input: ["Twitter", "postTweet", "getNewsFeed", "follow", "postTweet", "getNewsFeed", "unfollow", "getNewsFeed"] [[], [1, 5], [1], [1, 2], [2, 6], [1], [1, 2], [1]]
  • Output: [null, null, [5], null, null, [6, 5], null, [5]]
  • Explanation:
    • Twitter twitter = new Twitter();
    • twitter.postTweet(1, 5); // User 1 posts a new tweet (id = 5)
    • twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5]
    • twitter.follow(1, 2); // User 1 follows user 2
    • twitter.postTweet(2, 6); // User 2 posts a new tweet (id = 6)
    • twitter.getNewsFeed(1); // User 1's news feed should return a list with 2 tweet ids -> [6, 5]
    • twitter.unfollow(1, 2); // User 1 unfollows user 2
    • twitter.getNewsFeed(1); // User 1's news feed should return a list with 1 tweet id -> [5]
Example 2
  • Input: ["Twitter", "postTweet", "postTweet", "postTweet", "follow", "getNewsFeed"] [[], [1, 10], [1, 11], [2, 12], [1, 2], [1]]
  • Output: [null, null, null, null, null, [12, 11, 10]]
  • Explanation:
    • Twitter twitter = new Twitter();
    • twitter.postTweet(1, 10); // User 1 posts a new tweet (id = 10)
    • twitter.postTweet(1, 11); // User 1 posts a new tweet (id = 11)
    • twitter.postTweet(2, 12); // User 2 posts a new tweet (id = 12)
    • twitter.follow(1, 2); // User 1 follows user 2
    • twitter.getNewsFeed(1); // User 1's news feed should return the 3 tweets -> [12, 11, 10]
Constraints
  • 1 <= userId, followerId, followeeId <= 500
  • 0 <= tweetId <= 10^4
  • All the tweets have unique IDs.
  • At most 3 * 10^4 calls will be made to postTweet, getNewsFeed, follow, and unfollow.
  • Time Complexity: O(N log K) for getNewsFeed operation where N is the total number of tweets and K is the feed size (10)
  • Space Complexity: O(U + E + T) where U is the number of users, E is the number of follow relationships, and T is the number of tweets
Hash MapPriority QueueAmazonGoogle
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a hash map to store user follow relationships.
  • Use a hash map to store user tweets with timestamps.
  • Use a priority queue (max heap) for efficiently merging multiple user tweet streams.
  • Consider using a linked list or vector for maintaining each user's tweets.
  • Remember to maintain chronological order of tweets using timestamps.

Steps to solve by this approach:

 Step 1: Define the data structures to store tweets and follow relationships
 Step 2: Implement postTweet to add tweets with timestamps
 Step 3: Implement follow/unfollow operations using sets for efficient lookups
 Step 4: For getNewsFeed, collect tweets from the user and all followees
 Step 5: Use a max heap to efficiently get the 10 most recent tweets by timestamp
 Step 6: Extract the top 10 tweets from the heap to form the feed
 Step 7: Ensure proper time complexity by using appropriate data structures

Submitted Code :