-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday15.cpp
More file actions
54 lines (44 loc) · 1.06 KB
/
Copy pathday15.cpp
File metadata and controls
54 lines (44 loc) · 1.06 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
/*
Good morning! Here's your coding interview problem for today.
This problem was asked by Facebook.
Given a stream of elements too large to store in memory, pick a random element from the stream with uniform probability.
*/
#include <gtest/gtest.h>
#include <stdlib.h>
using namespace std;
int r_sample(const vector<int> *stream)
{
/*
Idea: The algo solution is called reservoir sampling (k=1)
Let's represent the stream as a vector and not use it's size
*/
int i = 1;
auto it = stream->begin();
int reservoir = *it;
while (it != stream->end())
{
++it;
++i;
int index = rand() % i;
if (index == 0)
reservoir = *it;
}
return reservoir;
}
TEST(SAMPLE, r_sample)
{
srand(1);
const vector<int> s{-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 1000000; i++)
{
sum += r_sample(&s);
}
// It seems good
cout << sum << endl;
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}