Solve Leetcode Problems and Get Offers From Your Dream Companies||Tweets count per frequency
Tags: programming, career, algorithms, computerscience
Published: 2021-02-15 19:10:42

**Problem 1348. Tweet Counts Per Frequency.**
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/beo2kd84tzxu0i48naym.png)

In this series, I am going to solve Leetcode medium problems live with my friend, which you can see on our youtube channel, Today we will do Problem Problem 1348. **Tweet Counts Per Frequency.**

A little bit about me, I have offers from **Uber India** and **Amazon India** in the past, and I am currently working for **Booking.com** in Amsterdam.
## Problem Statement

Implement the class `TweetCounts` that supports two methods:

* `recordTweet(string tweetName, int time)`

   * Stores the `tweetName` at the recorded `time` (in seconds).

* `getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime)`

  * Returns the total number of occurrences for the given      `tweetName` per minute, hour, or day (depending on `freq`) starting from the `startTime` (in seconds) and ending at the `endTime` (in seconds).
  * `freq` is always minute, hour or day, representing the time interval to get the total number of occurrences for the given tweetName.
  * The first time interval always starts from the `startTime`, so the time intervals are `[startTime, startTime + delta*1>, [startTime + delta*1, startTime + delta*2>, [startTime + delta*2, startTime + delta*3>, ... , [startTime + delta*i, min(startTime + delta*(i+1), endTime + 1)>` for some non-negative number `i` and `delta` (which depends on `freq`).

### Example:

**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]]`

**Output**

`[null,null,null,null,[2],[2,1],null,[4]]`

**Explanation**

`TweetCounts tweetCounts = new TweetCounts();
tweetCounts.recordTweet("tweet3", 0);
tweetCounts.recordTweet("tweet3", 60);
tweetCounts.recordTweet("tweet3", 10);                             // All tweets correspond to "tweet3" with recorded times at 0, 10 and 60.
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // return [2]. The frequency is per minute (60 seconds), so there is  one interval of time: 1) [0, 60> - > 2 tweets.
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // return [2, 1]. The frequency is per minute (60 seconds), so there are two intervals of time: 1) [0, 60> - > 2 tweets, and 2) [60,61> - > 1 tweet.
tweetCounts.recordTweet("tweet3", 120);                            // All tweets correspond to "tweet3" with recorded times at 0, 10, 60 and 120.
tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210);  // return [4]. The frequency is per hour (3600 seconds), so there is one interval of time: 1) [0, 211> - > 4 tweets.`


## Youtube Discussion

  {% youtube eOX9rYomKac %}

## Solution

In this problem, we have to implement two methods. On is recordTweet which stores the tweet along with its timestamp and another one is getTweetsPerFrequenecy where we have to return no of tweets in some given frequency (minute, hour, day).

Here we will be using a HashMap to store the tweet data. The key is the title of the tweet and the value is a list which contains all the timestamps where this tweet is published.

In the recordTweet method, we store the tweet into the hashmap. This method runs in O(1) time complexity as the retrieval of the list from hashmap takes constant time.

The input frequency can be minute, hour and day. All the inputs are given in seconds. Therefore to calculate the frequency in a minute, hour and day terms we need to use different constants.

`minute = 60 seconds`

`hour = 60 minutes = 3600 seconds`

`day = 24 hours = 24*3600 seconds`

The getFreq method is used to calculate the occurrences of the tweet in different timeframes. We can visualize the problem as placing the tweets into different buckets. The bucket size depends on the frequency (minute, hour, day). We will find the unit timeframes in seconds depending upon the frequency.

`eg : Start Time = 135 , End Time = 345 , freq = minutes`

`The bucket size will be 135-194 | 195-254 | 255-314 | 315-344`

So we will place the tweets with different timestamps into these buckets. The index is found out by using the following formula :

`index = (timestamp — startTime)/delta ; delta depends on the frequency`

The final list is return in the method.

The java code along with a dry run of the example is given below. Also, the code is properly documented.

{% gist https://gist.github.com/sksaikia/938400ca4be48ca546b6b9fdacf05fea.js %}

The code for this problem can be found in the following repository.
{% github webtutsplus/Leetcode %}

**Thank You for reading and Follow this publication for more LeetCode problems!😃**
[Leetcode simplified](https://medium.com/leetcode-simplified)


**Sign up for Leetcode Simplified**
By LeetCode Simplified

Get latest leetcode solution with youtube breakdown [Take a look](https://medium.com/leetcode-simplified/newsletters/leetcode-simplified?source=newsletter_v3_promo--------------------------newsletter_v3_promo-----------)
You're an editor of Leetcode Simplified