Design a hit counter which counts the number of hits received in the past 5 minutes.
Each function accepts a timestamp parameter (in seconds granularity) and you may assume that calls are being made to the system in chronological order (ie, the timestamp is monotonically increasing). You may assume that the earliest timestamp starts at 1.
It is possible that several hits arrive roughly at the same time.
Example:
HitCounter counter = new HitCounter(); // hit at timestamp 1. counter.hit(1); // hit at timestamp 2. counter.hit(2); // hit at timestamp 3. counter.hit(3); // get hits at timestamp 4, should return 3. counter.getHits(4); // hit at timestamp 300. counter.hit(300); // get hits at timestamp 300, should return 4. counter.getHits(300); // get hits at timestamp 301, should return 3. counter.getHits(301);
Follow up:
What if the number of hits per second could be very large? Does your design scale?
What if the number of hits per second could be very large? Does your design scale?
Update 2016-11-27
For follow up, keep two arrays of size 300: hits and timestamps. Hits array is used to track hits. Timestamps array is used to track the timestamp of the hits. Since total size is 300, we store timestamp of a hit in the cell with index timestamp % 300. Whenever there is a new hit, we first check if the corresponding cell in timestamps array stores the same timestamp as current timestamp, if it does, we increment hits array. Otherwise it means we have exceeded 300 seconds limit, so we need to reset the corresponding cell in hits array to 1. When calling getHits, we need to go through the timestamps array, find all hits that are within 300 seconds.
public class HitCounter { final int[] timestamps; final int[] hit; private static final int TIME = 300; public HitCounter () { timestamps = new int[TIME]; hit = new int[TIME]; } public void hit(int timestamp) { int residue = timestamp % TIME; if (timestamps[residue] == timestamp) { hit[residue]++; } else { hit[residue] = 1; timestamps[residue] = timestamp; } } public int getHits(int timestamp) { int hits = 0; for (int i = 0; i < TIME; i++) { if (timestamp - timestamps[i] < 300) { hits += hit[i]; } } return hits; } }
Use a queue to store timestamp, whenever the difference between timestamp on getHits and head of the queue is greater than 300 seconds, we remove the head timestamp from queue. Return queue size as number of hits.
public class HitCounter { private final Queue<integer> hitsQueue; public HitCounter () { this.hitsQueue = new LinkedList<>(); } /** Record a hit. @param timestamp - The current timestamp (in seconds granularity). */ public void hit(int timestamp) { hitsQueue.add(timestamp); } /** Return the number of hits in the past 5 minutes. @param timestamp - The current timestamp (in seconds granularity). */ public int getHits(int timestamp) { while (timestamp - hitsQueue.peek() >= 300) { hitsQueue.poll(); } return hitsQueue.size(); } }
No comments:
Post a Comment