Design Hit Counter
Updated
Problem statement
Build a hit counter for a website. It records hits and can tell how many hits happened in the last five minutes. Time is measured in whole seconds.
The calls come as two arrays of the same length. Call i is operations[i] at second timestamps[i], and you handle the calls in order:
"hit": one hit happens at that second. Several hits can happen in the same second."getHits": report how many hits happened in the last300seconds. For a call at secondt, count every hit from secondt - 299through secondt, both included.
A "getHits" call only counts hits from calls that come before it in the list. A hit listed after it does not count, even when it has the same second.
Return an array with one count for each "getHits" call, in the order of the calls. A "hit" call adds nothing to the array.
Function
hitCounter(operations: String[], timestamps: int[]) → int[]Examples
Example 1
operations = ["hit","hit","getHits","hit","getHits","getHits"]timestamps = [10,20,25,309,309,320]return = [2,3,1]- At second
25, the window is seconds1through25. It holds the hits at10and20, so the count is2. - At second
309, the window is10through309. It holds the hits at10,20and309, so the count is3. - At second
320, the window is21through320. The hits at10and20are too old, so the count is1.
Example 2
operations = ["hit","getHits","hit","hit","getHits"]timestamps = [5,5,5,5,304]return = [1,3]- The first
"getHits"at second5sees only the one hit listed before it. The two hits listed after it come later, so the count is1. - At second
304, the window is5through304, so all three hits at second5still count. The count is3.
Example 3
operations = ["getHits","hit","getHits"]timestamps = [1,2000000000,2000000000]return = [0,1]The first call comes before any hit, so its count is 0. Then one hit happens at second 2000000000, and the last call counts it, so its count is 1.
Constraints
1 <= operations.length <= 10^4timestamps.length == operations.length- Each
operations[i]is"hit"or"getHits". 1 <= timestamps[i] <= 2 * 10^9timestampsnever goes down:timestamps[i] <= timestamps[i + 1].
More Charta Health interview questions
Similar questions at other companies
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Music Player Operation SequenceAtlassian · PHONE SCREENAtlassian interview questions
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- Implement a FIFO QueueOptiver · PHONE SCREENOptiver interview questions
- Zigzag Iterator Over Two ListsGoogle · PHONE SCREENGoogle interview questions
- Bounded Circular QueueDatadog · PHONE SCREENDatadog interview questions
- Round-Robin Iterator over IteratorsBridge · ONSITE INTERVIEWBridge interview questions
- Design Hit CounterNutanix · ONSITE INTERVIEWNutanix interview questions