Tweet Counts Per Frequency

MediumDesignbinary search

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Build TweetCounts, which logs tweets by name and time (in seconds) and reports activity histograms. - recordTweet(tweetName, time) logs one tweet. The same name may be logged many times, including at the same second. - getTweetCountsPerFrequency(freq, tweetName, startTime, endTime) splits the inclusive range [startTime, endTime] into consecutive chunks of 60 seconds ("minute"), 3600 seconds ("hour"), or 86400 seconds ("day"), and returns a list with the number of tweetName tweets in each chunk, in time order. Chunks are [startTime, startTime + size - 1], [startTime + size, startTime + 2*size - 1], and so on; the last chunk is cut off at endTime, so it may be shorter. The list therefore has (endTime - startTime) // size + 1 entries. A name that was never logged yields all zeros.

Examples

Input: ["TweetCounts","recordTweet","recordTweet","recordTweet","getTweetCountsPerFrequency","getTweetCountsPerFrequency","recordTweet","getTweetCountsPerFrequency"] [[],["go",0],["go",60],["go",10],["minute","go",0,59],["minute","go",0,60],["go",120],["hour","go",0,210]]
Output: [null,null,null,null,[2],[2,1],null,[4]]
For [0,59] one chunk holds times 0 and 10. For [0,60] the chunks are [0,59] and [60,60], giving [2,1]. After logging 120, the single hour chunk [0,210] holds all 4 tweets.
Input: ["TweetCounts","recordTweet","recordTweet","recordTweet","getTweetCountsPerFrequency","getTweetCountsPerFrequency","getTweetCountsPerFrequency"] [[],["a",5],["a",5],["b",7],["minute","a",0,130],["minute","b",8,70],["day","c",0,10]]
Output: [null,null,null,null,[2,0,0],[0,0],[0]]
Chunks [0,59], [60,119], [120,130] contain 2, 0, 0 tweets named "a". Tweet "b" at 7 is before the range [8,70]. Name "c" was never logged.

Constraints

  • 0 <= time, startTime, endTime <= 10^9
  • 0 <= endTime - startTime <= 10^4
  • `freq` is one of `"minute"`, `"hour"`, `"day"`
  • At most 10^4 calls in total

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search problems