Skip to content

Instantly share code, notes, and snippets.

@alexradzin
Created June 15, 2022 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexradzin/6edaa0a75ac263a85d56ab50edcbf4b8 to your computer and use it in GitHub Desktop.
Save alexradzin/6edaa0a75ac263a85d56ab50edcbf4b8 to your computer and use it in GitHub Desktop.
Trivial twitter implementation
package com.sunbit;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.stream.Collectors.toList;
public class Twitter {
private final Map<Integer, List<Integer>> feeds = new HashMap<>();
private final Map<Integer, Collection<Integer>> followers = new HashMap<>();
/** Initialize your data structure here. */
public Twitter() {
}
/** Compose a new tweet. */
public void postTweet(int userId, int tweetId) {
feeds.merge(userId, new LinkedList<>(singletonList(tweetId)),
(existing, addition) -> Stream.concat(existing.stream(), addition.stream()).collect(toList()));
}
/** 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) {
return Stream.concat(Optional.ofNullable(followers.get(userId)).orElse(emptyList()).stream(), Stream.of(userId))
.flatMap(uid -> getFeeds(uid).stream()).sorted((o1, o2) -> o2 - o1).limit(10).collect(Collectors.toList());
}
private List<Integer> getFeeds(int userId) {
return Optional.ofNullable(feeds.get(userId)).orElse(emptyList());
}
/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
public void follow(int followerId, int followeeId) {
followers.merge(followerId, new LinkedList<>(singletonList(followeeId)), (existing, addition) -> new LinkedList<>(Stream.concat(existing.stream(), addition.stream()).collect(toList())));
}
/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
public void unfollow(int followerId, int followeeId) {
Optional.ofNullable(followers.get(followerId)).ifPresent(followees -> followees.remove(followeeId));
}
}
package com.sunbit;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class TwitterTest {
private final Twitter twitter = new Twitter();
@Test
void empty() {
assertTrue(twitter.getNewsFeed(1).isEmpty());
twitter.unfollow(1, 2); // nothing to check here: no users exist. We just verify that this method does not throw exception
}
@Test
void noFollowers() {
for (int i = 0; i < 5; i++) {
twitter.postTweet(1, i);
}
assertEquals(Arrays.asList(4, 3, 2, 1, 0), (twitter.getNewsFeed(1)));
assertEquals(Collections.emptyList(), (twitter.getNewsFeed(2)));
}
@Test
void reader() {
twitter.follow(1, 2);
twitter.follow(1, 3);
twitter.postTweet(2, 1);
twitter.postTweet(2, 2);
twitter.postTweet(3, 3);
twitter.postTweet(3, 4);
assertEquals(Arrays.asList(4, 3, 2, 1), (twitter.getNewsFeed(1)));
}
@Test
void writer() {
twitter.follow(1, 2);
twitter.follow(1, 3);
twitter.postTweet(1, 1);
twitter.postTweet(1, 2);
twitter.postTweet(1, 3);
twitter.postTweet(1, 4);
assertEquals(Arrays.asList(4, 3, 2, 1), (twitter.getNewsFeed(1)));
}
@Test
void test() {
// 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.
assertEquals(Arrays.asList(6, 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.
assertEquals(singletonList(5), (twitter.getNewsFeed(1)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment