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 (10000 in the above example).

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.

Examples

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
Input:
["TweetCounts","recordTweet","recordTweet","recordTweet","getTweetCountsPerFrequency","getTweetCountsPerFrequency","recordTweet","getTweetCountsPerFrequency"]
[[],["tweet3",0],["tweet3",60],["tweet3",10],["minute","tweet3",0,59],["minute","tweet3",0,60],["tweet3",120],["hour","tweet3",0,210]]

Output:
[null,null,null,null,[2],[2,1],null,[4]]

Explanation:
TweetCounts tweetCounts = new TweetCounts();
tweetCounts.recordTweet("tweet3", 0);                              // New tweet "tweet3" at time 0
tweetCounts.recordTweet("tweet3", 60);                             // New tweet "tweet3" at time 60
tweetCounts.recordTweet("tweet3", 10);                             // New tweet "tweet3" at time 10
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 59); // return [2]; chunk [0,59] had 2 tweets
tweetCounts.getTweetCountsPerFrequency("minute", "tweet3", 0, 60); // return [2,1]; chunk [0,59] had 2 tweets, chunk [60,60] had 1 tweet
tweetCounts.recordTweet("tweet3", 120);                            // New tweet "tweet3" at time 120
tweetCounts.getTweetCountsPerFrequency("hour", "tweet3", 0, 210);  // return [4]; chunk [0,210] had 4 tweets

Constraints

  • 0 <= time, startTime, endTime <= 10^9
  • 0 <= endTime - startTime <= 10^4
  • There will be at most 104 calls in total to recordTweet and getTweetCountsPerFrequency.

Solution

Intuition

Store tweet times in a map. For each query, use binary search to count tweets in each chunk.

Approach

  1. Store times for each tweetName in a sorted list.
  2. For getTweetCountsPerFrequency, determine chunk size, and for each chunk, count tweets using binary search.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.*;
class TweetCounts {
    Map<String, List<Integer>> map = new HashMap<>();
    public TweetCounts() {}
    public void recordTweet(String tweetName, int time) {
        map.computeIfAbsent(tweetName, k -> new ArrayList<>()).add(time);
    }
    public List<Integer> getTweetCountsPerFrequency(String freq, String tweetName, int startTime, int endTime) {
        int chunk = freq.equals("minute") ? 60 : freq.equals("hour") ? 3600 : 86400;
        List<Integer> times = map.getOrDefault(tweetName, new ArrayList<>());
        Collections.sort(times);
        List<Integer> res = new ArrayList<>();
        for (int s = startTime; s <= endTime; s += chunk) {
            int e = Math.min(s + chunk - 1, endTime);
            int left = lowerBound(times, s);
            int right = lowerBound(times, e + 1);
            res.add(right - left);
        }
        return res;
    }
    private int lowerBound(List<Integer> arr, int target) {
        int l = 0, r = arr.size();
        while (l < r) {
            int m = (l + r) / 2;
            if (arr.get(m) < target) l = m + 1;
            else r = m;
        }
        return l;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import bisect
class TweetCounts:
    def __init__(self):
        self.map = {}
    def recordTweet(self, tweetName, time):
        self.map.setdefault(tweetName, []).append(time)
    def getTweetCountsPerFrequency(self, freq, tweetName, startTime, endTime):
        chunk = {'minute': 60, 'hour': 3600, 'day': 86400}[freq]
        times = sorted(self.map.get(tweetName, []))
        res = []
        for s in range(startTime, endTime+1, chunk):
            e = min(s + chunk - 1, endTime)
            l = bisect.bisect_left(times, s)
            r = bisect.bisect_right(times, e)
            res.append(r - l)
        return res

Complexity

  • ⏰ Time complexity: O(q log q) per query, where q is the number of tweets for the name.
  • 🧺 Space complexity: O(n) — For storing all tweet times.