Tweet Counts Per Frequency - Problem
Design a Tweet Analytics System ๐
A social media company needs to monitor activity on their platform by analyzing tweet frequencies over different time periods. Your task is to build an efficient API that can record tweets and query tweet counts across customizable time intervals.
The Challenge: Given a time period
โข "minute": 60-second chunks
โข "hour": 3600-second chunks
โข "day": 86400-second chunks
Example: Period
โข Every minute:
โข Every hour:
โข Every day:
Note: The last chunk may be shorter and always ends at
Implement:
1.
2.
3.
A social media company needs to monitor activity on their platform by analyzing tweet frequencies over different time periods. Your task is to build an efficient API that can record tweets and query tweet counts across customizable time intervals.
The Challenge: Given a time period
[startTime, endTime], partition it into chunks based on frequency:โข "minute": 60-second chunks
โข "hour": 3600-second chunks
โข "day": 86400-second chunks
Example: Period
[10, 10000] seconds:โข Every minute:
[10,69], [70,129], [130,189], ..., [9970,10000]โข Every hour:
[10,3609], [3610,7209], [7210,10000]โข Every day:
[10,10000]Note: The last chunk may be shorter and always ends at
endTime.Implement:
1.
TweetCounts() - Initialize the system2.
recordTweet(tweetName, time) - Record a tweet at given time3.
getTweetCountsPerFrequency(freq, tweetName, startTime, endTime) - Return tweet counts per time chunk Input & Output
example_1.py โ Basic Operations
$
Input:
tweetCounts = TweetCounts()
tweetCounts.recordTweet("tweet3", 0)
tweetCounts.recordTweet("tweet3", 60)
tweetCounts.recordTweet("tweet3", 10)
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59)
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60)
โบ
Output:
[2]
[2, 1]
๐ก Note:
For the first query [0, 59], we have one chunk [0, 59] containing tweets at times 0 and 10, so count is 2. For the second query [0, 60], we have chunks [0, 59] and [60, 60], with counts 2 and 1 respectively.
example_2.py โ Hour Frequency
$
Input:
tweetCounts = TweetCounts()
tweetCounts.recordTweet("tweet1", 10)
tweetCounts.recordTweet("tweet1", 3610)
tweetCounts.recordTweet("tweet1", 7200)
tweetCounts.getTweetCountsPerFrequency("hour", "tweet1", 10, 10000)
โบ
Output:
[2, 1, 1]
๐ก Note:
With hour frequency, chunks are [10, 3609], [3610, 7209], [7210, 10000]. Tweet at 10 and 3610 fall in different chunks due to the hour boundary at 3610.
example_3.py โ Edge Case Empty
$
Input:
tweetCounts = TweetCounts()
tweetCounts.getTweetCountsPerFrequency("minute", "nonexistent", 0, 100)
โบ
Output:
[]
๐ก Note:
When querying a tweet name that doesn't exist, return an empty list since there are no recorded tweets for that name.
Visualization
Tap to expand
Understanding the Visualization
1
Record Tweets
As tweets arrive, insert them in sorted order by timestamp for each hashtag
2
Calculate Time Chunks
Divide the query time period into equal intervals based on frequency (minute/hour/day)
3
Binary Search Boundaries
For each chunk, use binary search to find first and last tweet in that time range
4
Count Efficiently
The difference between boundary indices gives the count instantly
Key Takeaway
๐ฏ Key Insight: By maintaining sorted timestamp arrays and using binary search for range queries, we transform a slow O(kรn) operation into an efficient O(k log n) solution - perfect for real-time social media analytics!
Time & Space Complexity
Time Complexity
O(k log n)
For each of k chunks, binary search takes O(log n) time to find range boundaries
โก Linearithmic
Space Complexity
O(n)
Store all n tweets in sorted lists within hash map
โก Linearithmic Space
Constraints
- 1 โค time, startTime, endTime โค 109
- 0 โค (endTime - startTime) โค 104
- freq is one of "minute", "hour", or "day"
- At most 1000 calls will be made to recordTweet and getTweetCountsPerFrequency
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code