-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path132_RiotGames_Design_HitCounter.py
executable file
·65 lines (45 loc) · 1.86 KB
/
132_RiotGames_Design_HitCounter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
This question was asked by Riot Games.
Design and implement a HitCounter class that keeps track of requests (or hits). It should support the following operations:
-> record(timestamp): records a hit that happened at timestamp
-> total(): returns the total number of hits recorded
-> range(lower, upper): returns the number of hits that occurred between timestamps lower and upper (inclusive)
Follow-up: What if our system has limited memory?
"""
import time
class HitCounter:
def __init__(self):
self.hit_time_stamp = {} # dict to store time_stamp: hit_list_index
self.hit_list = [] # to store hits for each time_stamp
def record(self, timestamp):
self.hit_time_stamp[timestamp] = len(self.hit_list)
self.hit_list.append(1)
def total(self):
return sum(self.hit_list)
def range(self, lower, upper):
return sum(self.hit_list[self.hit_time_stamp[lower]:self.hit_time_stamp[upper]+1])
# improved HitCounter, uses less memory
class HitCounterRedux:
def __init__(self):
self.hit_time_stamp = {} # dict to store time_stamp: total hits up till that point
# self.hit_list = [] # to store hits for each time_stamp
self.hit_counter = 0
def record(self, timestamp):
self.hit_counter += 1
self.hit_time_stamp[timestamp] = self.hit_counter
def total(self):
return self.hit_counter
def range(self, lower, upper):
return self.hit_time_stamp[upper] - self.hit_time_stamp[lower] + 1
if __name__ == '__main__':
player = HitCounterRedux() # HitCounter()
first = time.time()
last = None
player.record(first)
for i in range(5):
temp_time = time.time()
if i== 3:
last = temp_time
player.record(temp_time)
assert player.total() == 6
assert player.range(lower=first, upper=last) == 5