-
Notifications
You must be signed in to change notification settings - Fork 4
/
Google3Amazon2Walmart Global Tech2355. Design Twitter.java
90 lines (69 loc) · 2.3 KB
/
Google3Amazon2Walmart Global Tech2355. Design Twitter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Sounish Nath - 17.10.2023
class Tweet {
int tweetId;
int userId;
int clock;
Tweet(int ti, int ui, int c) {
tweetId=ti;
userId=ui;
clock=c;
}
}
class User {
int userId;
Set<Integer> followees=new HashSet<>();
LinkedList<Tweet> tweets=new LinkedList<>();
User (int ui) {
userId=ui;
}
void publishTweet(int twi, int c) {
tweets.addLast(new Tweet(twi, userId, c));
if (tweets.size() > 10) {
tweets.removeFirst();
}
}
}
class Twitter {
private Map<Integer, User> users=new HashMap<>();
private int clock=0; // monotonically increasing counter;
public Twitter() {
}
public void postTweet(int userId, int tweetId) {
users.putIfAbsent(userId, new User(userId));
users.get(userId).publishTweet(tweetId, ++clock);
}
public List<Integer> getNewsFeed(int userId) {
users.putIfAbsent(userId, new User(userId));
User user=users.get(userId);
PriorityQueue<Tweet> pq=new PriorityQueue<>((a,b) -> {
return b.clock - a.clock; // highest value on TOP ==> Mxheap
});
for (Tweet tweet : user.tweets) {
pq.offer(tweet);
}
// go all of her followees and put their tweets into pq
for (Integer fId : user.followees) {
User followee=users.get(fId);
// grab all of tweets
for (Tweet tweet : followee.tweets) {
pq.offer(tweet);
}
}
// take the top 10. from pq
List<Integer> tws=new ArrayList<>();
for (int i=0; i < 10 && !pq.isEmpty(); i++) {
tws.add(pq.poll().tweetId);
}
return tws;
}
public void follow(int followerId, int followeeId) {
users.putIfAbsent(followerId, new User(followerId));
users.putIfAbsent(followeeId, new User(followeeId));
users.get(followerId).followees.add(followeeId);
}
public void unfollow(int followerId, int followeeId) {
users.putIfAbsent(followerId, new User(followerId));
users.putIfAbsent(followeeId, new User(followeeId));
users.get(followerId).followees.remove(followeeId);
}
}