Design Twitter

LeetCode Q 355 - 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:

  1. postTweet(userId, tweetId): Compose a new tweet.
  2. 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.
  3. follow(followerId, followeeId): Follower follows a followee.
  4. 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

Method 1: HashTable

Code:

private static int timeStamp = 0;
private Map<Integer, Tweet> tweetMap;
private Map<Integer, Set<Integer>> userMap;

class Tweet {   // increase an time field to the Tweet class
	int tweetId; // enable the sorting by time function
	int time;
	Tweet next;  // in the tweetMap, we only store the most recent tweet and use next attribute to find its next recent tweet
	public Tweet(int id, int time) {
		this.tweetId = id; this.time = time; this.next = null;
	}
}

// Initialize your data structure here.
	public Twitter() {
		this.tweetMap = new HashMap<>();
		this.userMap = new HashMap<>();
	}

// Compose a new tweet. 
public void postTweet(int userId, int twitId) {
	Tweet curr = new Tweet(twitId, ++timeStamp);
	curr.next = tweetMap.getOrDefault(userId, null);
	tweetMap.put(userId, curr);
}

// 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) {
	List<Integer> res = new ArrayList<>();
	Set<Integer> set = new HashSet<>();
	set.add(userId); // add the user itself
	if (userMap.containsKey(userId)) set.addAll(userMap.get(userId));

	Queue<Tweet> pq = new PriorityQueue<>((a,b) -> (b.time - a.time));
	for (int num: set) {
		if (tweetMap.get(num) != null) pq.offer(tweetMap.get(num));
	}

	int count = 0;
	while (!pq.isEmpty() && count < 10) {
		Tweet t = pq.poll();
		if (t.next != null) pq.offer(t.next);
		count++;
		res.add(t.tweetId);
	} 
	
	return res;
}

// Follower follows a followee. If the operation is invalid, it should be a no-op.
public void follow(int followerId, int followeeId) {
	userMap.putIfAbsent(followerId, new HashSet<>());
	userMap.get(followerId).add(followeeId);	
}   

// Follower unfollows a followee. If the operation is invalid, it should be a no-op. 
public void unfollow(int followerId, int followeeId) {
	if (userMap.get(followerId) == null) return;
	userMap.get(followerId).remove(followeeId);
}

Method 2: OOD (TODO)


   Reprint policy


《Design Twitter》 by Tong Shi is licensed under a Creative Commons Attribution 4.0 International License
  TOC