362. Design Hit Counter
Design Hit Counter (LeetCode 362) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.
How to solve Design Hit Counter
Design Hit Counter is a Data Structure Design problem, part of Design Patterns. Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.
Work towards the solution in these steps:
- Consider using a data structure that efficiently stores timestamps and allows quick access to recent hits.
- Since timestamps are in chronological order, think about a way to discard outdated hits as new ones come in to keep the data structure size manageable.
- A queue or deque can be useful here because it maintains order and allows removing old hits from the front efficiently.
The reference solution runs in O(1) amortized time and O(N) space.
The Design Hit Counter solution and editorial walks through the full approach with reference code.
Problem statement
You are given a class HitCounter that records hits at given timestamps (in seconds). Implement the following methods:
- hit(timestamp): Records a hit that happened at the given timestamp (an integer representing the current time in seconds). - getHits(timestamp): Returns the number of hits recorded in the past 5 minutes (i.e., in the time interval [timestamp - 299, timestamp], inclusive).
Both methods will be called with timestamps in chronological order (i.e., the timestamp parameter is always greater than or equal to the previous calls).
Return the number of hits in the past 5 minutes for each call to getHits.
Examples
Example 1
Input: {"operations": ["HitCounter", "hit", "hit", "getHits", "getHits"], "parameters": [[], [1], [2], [3], [301]]}
Output: [null, null, null, 2, 1]
Explanation: Two hits at timestamps 1 and 2, getHits at 3 returns 2, getHits at 301 returns 1 (only the hit at 2 is within the last 300 seconds).
Example 2
Input: {"operations": ["HitCounter", "hit", "hit", "getHits", "hit", "getHits", "getHits"], "parameters": [[], [1], [300], [300], [301], [301], [600]]}
Output: [null, null, null, 2, null, 2, 1]
Explanation: The hits at timestamps 1 and 300 are both counted at time 300, the hit at 301 keeps the count at 2, and by time 600 only the hit at 301 remains within the past 5 minutes.
Topics and companies
Topics: Array, Binary Search, Design, Queue, Data Stream.
Reported in interviews at Amazon, Microsoft, Apple, Bloomberg, Google, Yandex.
How to practise Design Hit Counter on Thita.ai
Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(1) amortized time and O(N) space.
Open Design Hit Counter in the code editor, or read the Design Hit Counter editorial for a worked solution with its approach and complexity analysis.
Where Design Hit Counter sits in the DSA pattern sheet
Problems related to Design Hit Counter
Other problems that use the same Design (General/Specific) and Design Patterns techniques:
- All O`one Data Structure — Hard, same subpattern
- Design a Stack With Increment Operation — Medium, same subpattern
- Design a Text Editor — Hard, same subpattern
- Design Circular Deque — Medium, same subpattern
- Design Circular Queue — Medium, same subpattern
- Design Compressed String Iterator — Easy, same subpattern
Design Hit Counter FAQ
What is the LeetCode number of Design Hit Counter?
Design Hit Counter is LeetCode problem 362, rated Medium.
What pattern is used to solve Design Hit Counter?
Design Hit Counter is a Data Structure Design problem. It belongs to the Design Patterns pattern, in the Design (General/Specific) subpattern.
What is the time complexity of Design Hit Counter?
The reference solution runs in O(1) amortized time and O(N) space.
Which companies ask Design Hit Counter?
Design Hit Counter has been reported in interviews at Amazon, Microsoft, Apple, Bloomberg, Google, Yandex.