Problem Description
Design a TweetCounts class that supports recording tweet occurrences and retrieving the number of tweets in specified time intervals. Given a tweet name and its posting time (in seconds), the API should return the count of tweets within a time range partitioned into chunks based on a given frequency (minute, hour, or day).
Key Insights
- Use a hashmap (or dictionary) to map tweet names to a list of their posting times.
- Determine the chunk size based on frequency: 60 seconds for a minute, 3600 seconds for an hour, and 86400 seconds for a day.
- Partition the time interval [startTime, endTime] into chunks and count the number of tweet timestamps falling into each chunk.
- Efficiently process queries by iterating over the recorded timestamps, optionally leveraging binary search if the timestamps are maintained in a sorted order.
Space and Time Complexity
Time Complexity: O(T + log N) per query where T is the number of tweets in the time range and N is the total number of tweets for a given tweet name. Space Complexity: O(N) for storing all tweet timestamps.
Solution
We implement the TweetCounts class with two methods: recordTweet and getTweetCountsPerFrequency. The recordTweet method stores the tweet by appending its timestamp to a list corresponding to the tweet name in a hashmap. For the getTweetCountsPerFrequency method, we determine the appropriate time chunk based on the frequency, compute the number of chunks, then iterate over the tweet timestamps that fall within the given time range, mapping each timestamp to its respective chunk index. This approach uses a hashmap to group timestamps and simple arithmetic to partition the time range, ensuring correct and efficient counting.
Code Solutions
Below are code solutions with line-by-line comments for each language.