#1348

Tweet Counts Per Frequency

specialist · 990 · lc medium +32 · 45.9% accepted · 220 likes · top 30%

Description

A social media platform analyzes tweet activity over time intervals. A time period can be split into equal-length buckets at a specified frequency (minute = 60s, hour = 3600s, day = 86400s). The last bucket may be shorter.

Implement the TweetCounts class:

- TweetCounts() Initializes the object.

- void recordTweet(String tweetName, int time) Records a tweet named tweetName at the given time (in seconds).

- List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime) For the interval [startTime, endTime] divided into buckets of the given freq, returns a list of tweet counts per bucket.


- freq is one of "minute", "hour", or "day".

Example 1:

Input
["TweetCounts","recordTweet","recordTweet","recordTweet","getTweetCountsPerFrequency","getTweetCountsPerFrequency","recordTweet","getTweetCountsPerFrequency"]
[[],["tweet3",0],["tweet3",60],["tweet3",10],["minute","tweet3",0,59],["minute","tweet3",0,60],["tweet3",120],["hour","tweet3",0,210]]

Example 2:

Output
[null,null,null,null,[2],[2,1],null,[4]]

Example 3:

Explanation
TweetCounts tweetCounts = new TweetCounts();
tweetCounts.recordTweet("tweet3", 0); // New tweet "tweet3" at time 0
tweetCounts.recordTweet("tweet3", 60); // New tweet "tweet3" at time 60
tweetCounts.recordTweet("tweet3", 10); // New tweet "tweet3" at time 10
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // return [2]; chunk [0,59] had 2 tweets
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // return [2,1]; chunk [0,59] had 2 tweets, chunk [60,60] had 1 tweet
tweetCounts.recordTweet("tweet3", 120); // New tweet "tweet3" at time 120
tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210); // return [4]; chunk [0,210] had 4 tweets

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16