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 [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 system
2. recordTweet(tweetName, time) - Record a tweet at given time
3. 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
๐Ÿš€ Tweet Analytics Dashboard๐Ÿ“Š Live Tweet Stream: #BreakingNewsTimestamps: [10, 25, 45, 67, 89, 120, 150, 180, 195, 220, 245, 300]๐Ÿ“ˆ Query RequestMinute chunks: [70, 189]๐Ÿ” Binary SearchFind range boundariesโšก Instant ResultChunk counts: [2, 2]Time Chunks Visualization:Chunk 1: [70, 129]Chunk 2: [130, 189]Sorted tweet times:1025456789120150180195220๐ŸŽฏ Key Insight: Sorted Storage + Binary SearchO(k log n) queries instead of O(kร—n) - perfect for real-time analytics!๐Ÿ’ก Essential for high-frequency social media monitoring systems
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Store all n tweets in sorted lists within hash map

n
2n
โšก 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
Asked in
Twitter 85 Meta 72 Google 65 Amazon 58
58.2K Views
High Frequency
~25 min Avg. Time
1.8K 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