Count Named Records in Minute, Hour or Day Buckets
Company: Intercom
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
### Problem
Implement TweetCounts. recordTweet(tweetName,time) stores one occurrence at an integer second. getTweetCountsPerFrequency(freq,tweetName,startTime,endTime) partitions the inclusive query interval into consecutive chunks beginning at startTime, with widths 60 for minute,3600 for hour or 86400 for day. The last chunk may be shorter. Return the occurrence count in every chunk, including zero counts. Records may arrive out of timestamp order.
```cpp
class TweetCounts {
public:
TweetCounts();
void recordTweet(string tweetName, int time);
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime);
};
```
### Examples
**Example 1:**
```text
Input: construct TweetCounts(); recordTweet("a",5); recordTweet("a",65); recordTweet("a",65); getTweetCountsPerFrequency("minute","a",5,65)
Output: [1,2]
Explanation: Chunks[5,64] and[65,65] contain one and two recorded occurrences.
```
**Example 2:**
```text
Input: construct TweetCounts(); recordTweet("b",100); recordTweet("a",110); getTweetCountsPerFrequency("hour","b",0,120); getTweetCountsPerFrequency("minute","c",0,60)
Output: query results[1] and[0,0]
Explanation: Names are counted separately, and an unrecorded name has zero occurrences in every chunk.
```
### Constraints
- time,startTime,endTime are integers in `[0,1000000000]`.
- `0 <= endTime-startTime <= 10000`.
- At most 10000 calls in total are made to recordTweet and getTweetCountsPerFrequency.
- freq is one of minute,hour,day as defined in the interface; the source supplies no additional tweet-name length bound.
### Contract considerations
Keep the native constructor and both methods. The example calls illustrate the object protocol rather than a new replay-array API.
```hint Read the boundary condition
Check duplicate timestamps, out-of-order records, and records at both query endpoints. Include zero-count chunks and the shortened final chunk.
```
### Boundary reminders
Keep the native constructor and both methods. The example calls illustrate the object protocol rather than a new replay-array API.
Overview: Count Named Records in Minute, Hour or Day Buckets. Implement TweetCounts. recordTweet(tweetName,time) stores one occurrence at an integer second.
Count Named Records in Minute, Hour or Day Buckets
Intercom
Jun 11, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0
Problem
Implement TweetCounts. recordTweet(tweetName,time) stores one occurrence at an integer second. getTweetCountsPerFrequency(freq,tweetName,startTime,endTime) partitions the inclusive query interval into consecutive chunks beginning at startTime, with widths 60 for minute,3600 for hour or 86400 for day. The last chunk may be shorter. Return the occurrence count in every chunk, including zero counts. Records may arrive out of timestamp order.
class TweetCounts {
public:
TweetCounts();
void recordTweet(string tweetName, int time);
vector<int> getTweetCountsPerFrequency(string freq, string tweetName, int startTime, int endTime);
};
Examples
Example 1:
Input: construct TweetCounts(); recordTweet("a",5); recordTweet("a",65); recordTweet("a",65); getTweetCountsPerFrequency("minute","a",5,65)
Output: [1,2]
Explanation: Chunks[5,64] and[65,65] contain one and two recorded occurrences.
Example 2:
Input: construct TweetCounts(); recordTweet("b",100); recordTweet("a",110); getTweetCountsPerFrequency("hour","b",0,120); getTweetCountsPerFrequency("minute","c",0,60)
Output: query results[1] and[0,0]
Explanation: Names are counted separately, and an unrecorded name has zero occurrences in every chunk.
Constraints
time,startTime,endTime are integers in
[0,1000000000]
.
0 <= endTime-startTime <= 10000
.
At most 10000 calls in total are made to recordTweet and getTweetCountsPerFrequency.
freq is one of minute,hour,day as defined in the interface; the source supplies no additional tweet-name length bound.
Contract considerations
Keep the native constructor and both methods. The example calls illustrate the object protocol rather than a new replay-array API.
Boundary reminders
Keep the native constructor and both methods. The example calls illustrate the object protocol rather than a new replay-array API.