Design Twitter
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. Your design should support the following methods:
- postTweet(userId, tweetId): Compose a new tweet.
- getNewsFeed(userId): Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
- follow(followerId, followeeId): Follower follows a followee.
- unfollow(followerId, followeeId): Follower unfollows a followee.
Example:
Twitter twitter = new Twitter(); // User 1 posts a new tweet (id = 5). twitter.postTweet(1, 5); // User 1's news feed should return a list with 1 tweet id -> [5]. twitter.getNewsFeed(1); // User 1 follows user 2. twitter.follow(1, 2); // User 2 posts a new tweet (id = 6). twitter.postTweet(2, 6); // User 1's news feed should return a list with 2 tweet ids -> [6, 5]. // Tweet id 6 should precede tweet id 5 because it is posted after tweet id 5. twitter.getNewsFeed(1); // User 1 unfollows user 2. twitter.unfollow(1, 2); // User 1's news feed should return a list with 1 tweet id -> [5], // since user 1 is no longer following user 2. twitter.getNewsFeed(1);
1 public class Twitter { 2 int feedMaxNum; 3 Map<Integer, Set<Integer>> followees; 4 Map<Integer, List<Tweet>> tweets; 5 private static int timeStamp = 0; 6 7 public Twitter() { 8 feedMaxNum = 10; 9 followees = new HashMap<>(); 10 tweets = new HashMap<>(); 11 } 12 13 public void postTweet(int userId, int tweetId) { 14 if (!tweets.containsKey(userId)) { 15 tweets.put(userId, new LinkedList<Tweet>()); 16 } 17 tweets.get(userId).add(0, new Tweet(tweetId, Twitter.timeStamp++)); // add new tweet on the first place 18 } 19 20 /** 21 * Retrieve the 10 most recent tweet ids in the user's news feed. Each item in 22 * the news feed must be posted by users who the user followed or by the user 23 * herself. Tweets must be ordered from most recent to least recent. 24 */ 25 public List<Integer> getNewsFeed(int userId) { 26 PriorityQueue<Tweet> feedHeap = new PriorityQueue<>((t1, t2) -> Integer.compare(t1.timePosted, t2.timePosted)); 27 Set<Integer> myFollowees = followees.getOrDefault(userId, new HashSet<>()); 28 myFollowees.add(userId); 29 30 for (int followeeId : myFollowees) { 31 List<Tweet> followeeTweets = tweets.get(followeeId); 32 if (followeeTweets == null) continue; 33 for (Tweet t : followeeTweets) { 34 feedHeap.offer(t); 35 if (feedHeap.size() > feedMaxNum) 36 feedHeap.poll(); 37 } 38 } 39 List<Integer> myFeed = new LinkedList<>(); 40 while (!feedHeap.isEmpty()) { 41 myFeed.add(0, feedHeap.poll().tweetId); 42 } 43 return myFeed; 44 } 45 46 public void follow(int followerId, int followeeId) { 47 if (!followees.containsKey(followerId)) 48 followees.put(followerId, new HashSet<Integer>()); 49 followees.get(followerId).add(followeeId); 50 } 51 52 public void unfollow(int followerId, int followeeId) { 53 if (followees.containsKey(followerId)) { 54 followees.get(followerId).remove(followeeId); 55 } 56 } 57 } 58 59 class Tweet { 60 int tweetId; 61 int timePosted; 62 63 public Tweet(int tId, int time) { 64 tweetId = tId; 65 timePosted = time; 66 } 67 }