Tweet Counts Per Frequency - Problem

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).

For example, the period [10, 10000] (in seconds) would be partitioned into the following time chunks with these frequencies:

  • Every minute (60-second chunks): [10,69], [70,129], [130,189], ..., [9970,10000]
  • Every hour (3600-second chunks): [10,3609], [3610,7209], [7210,10000]
  • Every day (86400-second chunks): [10,10000]

Notice that the last chunk may be shorter than the specified frequency's chunk size and will always end with the end time of the period.

Design and implement an API to help the company with their analysis.

Implement the TweetCounts class:

  • TweetCounts() Initializes the TweetCounts object.
  • void recordTweet(String tweetName, int time) Stores the tweetName at the recorded time (in seconds).
  • List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime) Returns a list of integers representing the number of tweets with tweetName in each time chunk for the given period of time [startTime, endTime] (in seconds) and frequency freq.

freq is one of "minute", "hour", or "day" representing a frequency of every minute, hour, or day respectively.

Input & Output

Example 1 — Basic Tweet Recording and Querying
$ Input: operations = ["TweetCounts","recordTweet","recordTweet","getTweetCountsPerFrequency"], params = [[],["tweet3",0],["tweet3",60],["minute","tweet3",0,59]]
Output: [null,null,null,[1]]
💡 Note: Initialize TweetCounts, record tweet3 at time 0, record tweet3 at time 60, then query minute frequency for tweet3 from 0 to 59. Only the tweet at time 0 falls in range [0,59].
Example 2 — Multiple Time Chunks
$ Input: operations = ["TweetCounts","recordTweet","recordTweet","recordTweet","getTweetCountsPerFrequency"], params = [[],["tweet3",0],["tweet3",60],["tweet3",120],["minute","tweet3",0,179]]
Output: [null,null,null,null,[1,1,1]]
💡 Note: Record tweets at times 0, 60, 120. Query minute frequency from 0 to 179 creates chunks [0,59],[60,119],[120,179], each containing 1 tweet.
Example 3 — Hour Frequency with Multiple Tweets
$ Input: operations = ["TweetCounts","recordTweet","recordTweet","recordTweet","getTweetCountsPerFrequency"], params = [[],["tweet1",100],["tweet1",3700],["tweet1",7300],["hour","tweet1",0,7199]]
Output: [null,null,null,null,[2]]
💡 Note: Record tweets at 100, 3700, 7300. Query hour frequency [0,7199] creates one chunk [0,3599] containing tweets at 100 and 3700 (7300 is outside range).

Constraints

  • 0 ≤ time ≤ 109
  • 0 ≤ startTime ≤ endTime ≤ 109
  • freq is one of "minute", "hour", or "day"
  • At most 104 calls to recordTweet and getTweetCountsPerFrequency

Visualization

Tap to expand
Tweet Counts Per Frequency: Recording and Queryingtweet3: [0,60]Recorded tweets[0,59]Chunk 1[60,119]Chunk 2[120,179]Chunk 3Count: 1Count: 1Count: 0Result: [1,1,0]Output arrayQuery: minute frequency for tweet3 from 0 to 179Each minute chunk (60 seconds) counted separatelyEfficient range queries with sorted timestamps!
Understanding the Visualization
1
Record Tweets
Store tweet timestamps grouped by name
2
Create Chunks
Divide time range into frequency-based chunks
3
Count Per Chunk
Return array of counts for each time chunk
Key Takeaway
🎯 Key Insight: Keep timestamps sorted per tweet name to enable fast binary search for range counting
Asked in
Google 15 Amazon 12 Facebook 8 Twitter 10
28.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen