Test cases need improvement


  • 0
    Q

    This code has a bug in unfollow() method, but still passes online judge.. See main mathod on the bottom for test case.

    public class Twitter {
    	private Map<Integer, Set<Integer>> userToFollowers = new HashMap<>();
    	private Map<Integer, TreeSet<Tweet>> newsFeeds = new HashMap<>();
    	private Map<Integer, LRUCache> usersLast10Tweets = new HashMap<Integer, LRUCache>();
    	int tweetIndex = 0;
    
    	public Twitter() {
    	}
    
    	public void postTweet(int userId, int tweetId) {
    		Set<Integer> followers = getFollowers(userId);
    		Tweet tweet = new Tweet(userId, tweetId, tweetIndex++);
    		for (int follower : followers)
    			getFeed(follower).add(tweet);
    
    		getLast10Tweets(userId).put(tweet, tweet);
    	}
    
    	public List<Integer> getNewsFeed(int userId) {
    		Set<Tweet> tweets = getFeed(userId);
    		List<Integer> result = new ArrayList<Integer>();
    		int count = 0;
    		for (Tweet tweet : tweets) {
    			if (count++ == 10)
    				break;
    			result.add(tweet.tweetId);
    		}
    		return result;
    	}
    
    	public void follow(int followerId, int followeeId) {
    		Set<Integer> followers = getFollowers(followeeId);
    		getFeed(followerId).addAll(getLast10Tweets(followeeId).keySet());
    		followers.add(followerId);
    	}
    
    	public void unfollow(int followerId, int followeeId) {
    		if (followerId == followeeId)
    			return;
    		getFollowers(followeeId).remove(followerId);	
    		/*
    		Iterator<Tweet> iter = getFeed(followerId).iterator();
        	while (iter.hasNext())
        		if (iter.next().userId==followeeId)
        			iter.remove();
        	*/
    		getFeed(followerId).removeAll(getLast10Tweets(followeeId).keySet());
    	}
    
    	private Set<Integer> getFollowers(Integer userId) {
    		Set<Integer> followers = userToFollowers.get(userId);
    		if (followers == null) {
    			followers = new HashSet<Integer>();
    			followers.add(userId);
    			userToFollowers.put(userId, followers);
    		}
    		return followers;
    	}
    
    	private TreeSet<Tweet> getFeed(Integer follower) {
    		TreeSet<Tweet> feed = newsFeeds.get(follower);
    		if (feed == null) {
    			feed = new TreeSet<Tweet>();
    			newsFeeds.put(follower, feed);
    		}
    		return feed;
    	}
    
    	private LRUCache getLast10Tweets(int userId) {
    		LRUCache cache = usersLast10Tweets.get(userId);
    		if (cache == null) {
    			cache = new LRUCache();
    			usersLast10Tweets.put(userId, cache);
    		}
    		return cache;
    	}
    
    	private class Tweet implements Comparable<Tweet> {
    		int tweetIndex;
    		int userId;
    		int tweetId;
    
    		public Tweet(int userId, int tweetId, int tweetIndex) {
    			this.userId = userId;
    			this.tweetId = tweetId;
    			this.tweetIndex = tweetIndex;
    		}
    
    		public int compareTo(Tweet o) {
    			return o.tweetIndex - this.tweetIndex;
    		}
    	}
    
    	private class LRUCache extends java.util.LinkedHashMap<Tweet, Tweet> {
    		public LRUCache() {
    			super(10, .75F, true);
    		}
    
    		protected boolean removeEldestEntry(Map.Entry<Tweet, Tweet> eldest) {
    			return size() > 10;
    		}
    	}
    
    	public static void main(String[] a) {
    		Twitter t = new Twitter();
    		t.follow(2, 1);
    		for (int i=0;i<20;i++){
    			t.postTweet(1,i);
    		}
    		t.unfollow(2, 1);
    		System.out.println(t.getNewsFeed(2));
    	}
    }

  • 0

    Thanks, I have added this test case and published it live.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.