Tweet Counts Per Frequency

A social media company is trying to monitor activity on their site by analyzing the number of tweets that occur in select periods of time. These periods can be partitioned into smaller time chunks based on a certain frequency (every minute, hour, or day).

Implement the TweetCounts class:

  • TweetCounts: Initializes the TweetCounts object.
  • recordTweet: Records the tweetName at the given time (in seconds).
  • getTweetCountsPerFrequency: Returns a list of integers representing the number of tweets for tweetName in each time chunk for the given period of time between startTime and endTime (in seconds), partitioned by the given frequency freq.

Example 1:

Input: ["TweetCounts", "recordTweet", "recordTweet", "recordTweet", "recordTweet", "getTweetCountsPerFrequency"]

[[], ["news", 10], ["news", 69], ["news", 70], ["news", 129], ["minute", "news", 10, 130]]

Output: [null, null, null, null, null, [2, 2, 0]]

Explanation: We record four "news" tweets at exactly the boundaries of 60-second intervals relative to our starting point of 10. When querying for "minute" frequency between times 10 and 130, the span is divided into 60-second chunks: [10, 69], [70, 129], and a final truncated chunk [130, 130]. The first two tweets (at 10 and 69) fall perfectly into the first chunk, yielding 2. The next two tweets (at 70 and 129) fall perfectly into the second chunk, yielding 2. The final chunk only covers the exact second 130, which contains no tweets, yielding 0.

Example 2:

Input: ["TweetCounts", "recordTweet", "recordTweet", "recordTweet", "getTweetCountsPerFrequency", "getTweetCountsPerFrequency"]

[[], ["sports", 0], ["sports", 3599], ["sports", 3600], ["hour", "sports", 0, 7200], ["minute", "sports", 3590, 3660]]

Output: [null, null, null, null, [2, 1, 0], [2, 0]]

Explanation: We log "sports" tweets at times 0, 3599, and 3600. First, we query by "hour" (3600-second chunks) from 0 to 7200. This creates chunks [0, 3599], [3600, 7199], and [7200, 7200]. The first two tweets land in the first hour block, the third tweet lands at the exact start of the second hour block, and the final block is empty, resulting in [2, 1, 0]. Next, we query the exact same data by "minute" (60-second chunks) from 3590 to 3660. This creates chunks [3590, 3649] and [3650, 3660]. The tweets at 3599 and 3600 both fall into the first minute chunk, while the second chunk is empty, resulting in [2, 0].

Now Your Turn!

Pick the correct output for the given input

Input: ["TweetCounts", "recordTweet", "recordTweet", "recordTweet", "getTweetCountsPerFrequency"]

[[], ["viral", 0], ["viral", 86400], ["viral", 172800], ["day", "viral", 0, 172800]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 0 <= time, startTime, endTime <= 109
  • 0 <= endTime - startTime <= 104
  • freq is guaranteed to be "minute", "hour", or "day".
  • At most 104 total calls will be made to recordTweet and getTweetCountsPerFrequency.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class TweetCounts {
public:
TweetCounts() {
// Your code goes here
}
void recordTweet(string tweetName, int time) {
// Your code goes here
}
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime) { // Your code goes here
}
};
 
Test Case

Input:

Methods
Params