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);
Solution
public class Twitter {
private Map<Integer, Set<Integer>> followeeMap;
private Map<Integer, List<Tweet>> messageMap;
private Map<Integer, List<Tweet>> ownTweets;
private int counter;
private static class Tweet {
int userId;
int messageId;
int timestamp;
public Tweet(int userId, int messageId, int timestamp) {
this.userId = userId;
this.messageId = messageId;
this.timestamp = timestamp;
}
}
/**
* Initialize your data structure here.
*/
public Twitter() {
followeeMap = new HashMap<>();
messageMap = new HashMap<>();
ownTweets = new HashMap<>();
counter = 0;
}
/**
* Compose a new tweet.
*/
public void postTweet(int userId, int tweetId) {
counter++;
if (!messageMap.containsKey(userId)) {
messageMap.put(userId, new ArrayList<>());
}
Tweet tweet = new Tweet(userId, tweetId, counter);
messageMap.get(userId).add(0, tweet);
if (!ownTweets.containsKey(userId)) {
ownTweets.put(userId, new ArrayList<>());
}
ownTweets.get(userId).add(tweet);
if (followeeMap.containsKey(userId)) {
followeeMap.get(userId).forEach(
eachUser -> {
if (!messageMap.containsKey(eachUser)) {
messageMap.put(eachUser, new ArrayList<>());
}
messageMap.get(eachUser).add(0, tweet);
}
);
} else {
followeeMap.put(userId, new HashSet<>());
}
}
/**
* 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.
*/
public List<Integer> getNewsFeed(int userId) {
if (!messageMap.containsKey(userId)) {
messageMap.put(userId, new ArrayList<>());
}
List<Tweet> tweets = messageMap.get(userId);
List<Integer> result = new ArrayList<>();
for (int i = 0; i <= 9; i++) {
if (i < tweets.size()) {
result.add(tweets.get(i).messageId);
}
}
return result;
}
/**
* Follower follows a followee. If the operation is invalid, it should be a no-op.
*/
public void follow(int followerId, int followeeId) {
if (followeeId == followerId) {
return;
}
if (!followeeMap.containsKey(followeeId)) {
followeeMap.put(followeeId, new HashSet<>());
}
if (followeeMap.get(followeeId).contains(followerId)) {
return;
}
followeeMap.get(followeeId).add(followerId);
// add tweets
if (!ownTweets.containsKey(followeeId)) {
ownTweets.put(followeeId, new ArrayList<>());
}
if (!messageMap.containsKey(followerId)) {
messageMap.put(followerId, new ArrayList<>());
}
List<Tweet> followerTweets = messageMap.get(followerId);
List<Tweet> followeeTweets = ownTweets.get(followeeId);
for (Tweet tweet : followeeTweets) {
insert(followerTweets, tweet);
}
}
private void insert(List<Tweet> followerTweets, Tweet tweet) {
int i = 0;
for( i = 0; i < followerTweets.size(); i ++) {
if (followerTweets.get(i).timestamp < tweet.timestamp) {
break;
}
}
followerTweets.add(i, tweet);
}
/**
* Follower unfollows a followee. If the operation is invalid, it should be a no-op.
*/
public void unfollow(int followerId, int followeeId) {
if (followeeId == followerId) {
return;
}
if (!followeeMap.containsKey(followeeId)) {
followeeMap.put(followeeId, new HashSet<>());
}
if (!followeeMap.get(followeeId).contains(followerId)) {
return;
}
followeeMap.get(followeeId).remove(followerId);
// remove tweets
List<Tweet> newTweets = new ArrayList<>();
if (!messageMap.containsKey(followerId)) {
messageMap.put(followerId, new ArrayList<>());
}
List<Tweet> tweets = messageMap.get(followerId);
for (Tweet tweet : tweets) {
if (tweet.userId == followeeId) {
} else {
newTweets.add(tweet);
}
}
messageMap.put(followerId, newTweets);
}
}
/**
* Your Twitter object will be instantiated and called as such:
* Twitter obj = new Twitter();
* obj.postTweet(userId,tweetId);
* List<Integer> param_2 = obj.getNewsFeed(userId);
* obj.follow(followerId,followeeId);
* obj.unfollow(followerId,followeeId);
*/