-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasy_Prob132.cpp
More file actions
56 lines (47 loc) · 1.73 KB
/
Easy_Prob132.cpp
File metadata and controls
56 lines (47 loc) · 1.73 KB
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
/* 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? */
#include <iostream>
using namespace std;
static const int MAX_SIZE = 1024;
class HitCounter{
protected :
float m_hitRegister[MAX_SIZE] ;
int m_index = 0;
int m_total = 0;
public:
// records a hit that happened at timestamp
void record(float timestamp){
m_total += 1;
m_hitRegister[m_index] = timestamp;
m_index = (m_index + 1)%MAX_SIZE;
}
// Returns the total number of hits recorded
int total(){
return m_total;
}
// Returns the number of hits that occured betweeen timsetamps lower and upper (inclusive)
int range(float lower, float upper){
int total = 0;
int limit = min(m_total, MAX_SIZE);
for (int i=0; i<limit; i++){
float timestamp = m_hitRegister[i];
if ((timestamp >= lower) && (timestamp <= upper)){
total += 1;
}
}
return total;
}
};
int main(int argc, char *argv[])
{
// HitCounter hitCounter ;
// cout << hitCounter.total() << endl;
// hitCounter.record(0.25);
// cout << hitCounter.total() << endl;
// hitCounter.record(0.60);
// cout << hitCounter.range(0.0,0.50) << endl;
// cout << hitCounter.range(0.0,1.0) << endl;
};