import random
class Solution(object):
def __init__(self):
"""
complete the constructor if needed.
"""
self.count = 0
self.sample_val =0
def read(self, value):
"""
read a value in the stream.
:type: value: int
"""
self.count += 1
# randomly select previous n data point
index = random.randint(0, self.count -1)
# the data point has 1/n possibility to be replaced by new value
if index == 0:
self.sample_val = value
return self.sample_val
def sample(self):
"""
return the sample of already read values.
:rtype: int
"""
return self.sample_val