← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
`python from collections import Counter from scipy import stats # This is a bonus, showing knowledge of statistical libraries
def verify_sampling(n: int, k: int, num_trials: int = 100000): """ Verifies the uniformity of Reservoir Sampling. n: Total size of the stream (e.g., numbers from 0 to n-1 in the stream) k: Sample size num_trials: Number of simulations """ counts = Counter()
for _ in range(num_trials):
# Generate a new stream (0 to n-1) for each trial
stream = iter(range(n))
sample = reservoir_sampling(stream, k)
for num in sample:
counts[num] += 1
# --- Analyze Results ---
print(f"Total elements: {n}, Sample size: {k}, Trials: {num_trials}")
expected_count = num_trials * (k / n)
print(f"Expected count per element: {expected_count}")
# Prepare data for Chi-Square Test
observed_frequencies = []
expected_frequencies = []
max_error = 0
print("\n--- Sample Frequencies ---")
# Check the frequency of each number (0 to n-1)
for i in range(n):
obs = counts[i]
observed_frequencies.append(obs)
expected_frequencies.append(expected_count)
error_pct = abs(obs - expected_count) / expected_count * 100
max_error = max(max_error, error_pct)
if i < 5 i > n - :
()
()
chi2_stat, p_val = stats.chisquare(f_obs=observed_frequencies, f_exp=expected_frequencies)
()
()
()
p_val > :
()
:
()
`