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 Binary Search Approach INPUT class TweetCounts HashMap: tweetName -> timestamps Operations Sequence: 1 TweetCounts() 2 recordTweet("tweet3",0) 3 recordTweet("tweet3",60) 4 getTweetCountsPerFrequency ("minute","tweet3",0,59) Timeline (seconds): t=0 t=60 Query: [0, 59] (1 minute chunk) ALGORITHM STEPS 1 Store Tweets HashMap with sorted list "tweet3" -> [0, 60] (sorted timestamps list) 2 Calculate Chunks delta = 60s for "minute" chunks = (59-0)/60 + 1 = 1 Range: [0, 59] = 1 chunk 3 Binary Search Find tweets in each chunk Chunk [0,59]: lower_bound(0) = idx 0 upper_bound(59) = idx 1 4 Count per Chunk count = upper - lower count = 1 - 0 = 1 FINAL RESULT HashMap State: tweetMap = { "tweet3": [0, 60] } Query Result: Frequency: minute (60 seconds) Time Range: 0 to 59 Only t=0 falls in [0, 59] Output: [1] OK - 1 tweet in chunk [0,59] t=60 excluded (outside range) Key Insight: Binary Search Optimization Using binary search (lower_bound/upper_bound) allows O(log n) counting per chunk instead of O(n). Store timestamps in sorted order. For each time chunk, find range boundaries with binary search, then count = upper_index - lower_index. Total: O(k log n) where k = number of chunks. TutorialsPoint - Tweet Counts Per Frequency | Binary Search Approach
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