← Questions
Gaps between middle order statistics of uniform samples
0
I was asked this
No reports yet. Sign in to say where you were asked it.
Draw ten numbers uniformly from [0, 1] and sort them. Look at two gaps: between the 4th and 5th smallest values, and between the 5th and 6th. Simulate many draws, collect both gaps for each draw to get their empirical distributions, then discuss how you would visualize them, test whether the two distributions are the same, and what theory predicts.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
middle_gaps(samples: list[list[float]]) -> tuple[list[float], list[float]]— for each row of ten values, sort a copy and returnsorted[4] - sorted[3]in the first list andsorted[5] - sorted[4]in the second, in row order. Don't changesamples. -
simulate(trials: int, seed: int) -> list[list[float]]— draws the rows.
Example
middle_gaps([[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]]) -> ([0.1], [0.1])
middle_gaps([[0.9, 0.1, 0.4, 0.8, 0.2, 0.7, 0.3, 0.6, 0.0, 0.5],
[0.5] * 10]) -> ([0.1, 0.0], [0.1, 0.0])
middle_gaps([[0.0, 0.0, 0.0, 0.0, 0.25, 0.75, 1.0, 1.0, 1.0, 1.0]]) -> ([0.25], [0.5])
With 200,000 simulated draws, both gaps average about 0.0909, close to 1/11.
Constraints
- Up to 100,000 rows, each with exactly ten finite values in
[0, 1]. - Compare floats with a tolerance of about
10^-9.
Clarifying questions
- Are the samples given, or do I generate them, and with what seed?
- Is the goal the two arrays, the comparison, or both?
- Which test is expected: a two-sample test, or a comparison against a known distribution?
What a strong answer covers
- A vectorized version (sort along rows with NumPy) and its cost,
O(rows · 10 log 10). - Theory: the eleven spacings of ten uniform points are exchangeable, so both gaps have the same distribution,
Beta(1, 10), with mean1/11and density10(1 - x)^9. - Visuals: overlaid histograms or density estimates, empirical CDFs on one plot, and a Q-Q plot of one gap against the other.
- Testing: a two-sample Kolmogorov-Smirnov test, noting that the two gaps from one draw are dependent (negatively correlated), so pairing or using separate draws matters; or a goodness-of-fit test against
Beta(1, 10). - How the number of trials sets the precision of the estimate.
Follow-ups
- What changes for gaps between the 1st value and 0, or the 10th and 1?
- Samples from a normal distribution instead of a uniform one: are the middle gaps still equal in distribution?
- Estimate the correlation between the two gaps and compare with theory.
What it tests
statistics · sorting · simulation
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding