Streaming Quantile Estimation with Bounded Rank Error
Statistics & ExperimentsModel answerLast updated
Build a space-efficient quantile estimator for unbounded streams and explain deterministic rank guarantees versus probabilistic centroid methods.
You need to estimate quantiles from an incoming stream of numbers where storing every value is infeasible. Design an algorithm that can answer queries like "what is the 95th percentile?" while using limited memory.
Your solution should address:
Part 1: Deterministic Rank-Bounded Summary
Design a data structure that maintains a compressed representation of the stream. After observing N values, a quantile query for q should return a value whose true rank falls within ε·N positions of the ideal rank q·N. Explain your invariant, how updates preserve it, and how queries extract an answer.
Part 2: Centroid-Based Approach
Describe an alternative using weighted centroids (like t-digest) that cluster nearby values and adjust granularity by region. Explain how centroids are chosen, how scale functions affect tail versus center accuracy, and what guarantees this approach provides.
Part 3: Comparison
Compare both methods across:
- Memory usage and update time complexity
- Behavior on duplicate values and skewed distributions
- Accuracy in tails versus center regions
- Whether independently-built summaries can be merged while preserving guarantees
Clarifications
- Values arrive one at a time in arbitrary order
- Stream length is unknown in advance
- 0 ≤ q ≤ 1 represents the target quantile (e.g., 0.5 for median)
- ε defines the acceptable rank error as a fraction of N
- Exact min/max should be tracked for q=0 and q=1
- Distinguish between rank error (position) and value error (magnitude)
Hints
Hint 1
Think about maintaining a subset of observations with known rank relationships
Hint 2
Consider how merging two summaries affects the rank guarantees of each element
Hint 3
For centroids, the weight distribution controls where precision is concentrated
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Model Answer
Defining the Error Guarantee
A rank error of ±ε for quantile q means: after N observations, if the true qth quantile has rank r = ⌊q·N⌋, the returned value must have actual rank between (q-ε)·N and (q+ε)·N. This says nothing about numeric distance without distribution assumptions.
Part 1: Deterministic Summary (GK-style)
Core Idea: Maintain a sorted sample with explicit rank intervals. Each stored element carries bounds on how many unseen values could lie before it.
Data Structure: A sequence of tuples (value, g, Δ) where:
valueis an observed numbergis the minimum rank gap from the previous elementΔis the maximum additional rank uncertainty
The invariant: for every tuple, g + Δ ≤ 2εN ensures we can pinpoint any rank within εN positions.
Updates:
- Insert the new value into the sorted sequence
- Compute its rank bounds based on neighbors
- Compress: remove elements whose deletion keeps all remaining bounds within 2εN
- This compression happens periodically (every 1/2ε observations)
Queries: For quantile q, find the element whose minimum rank ≤ qN and maximum rank ≥ qN. The invariant guarantees this exists and has rank error ≤ εN.
Complexity: O(1/ε) space, O(log(1/ε)) update time. Works identically for duplicates since rank is position-based.
Merge Limitation: Concatenating two summaries and recompressing does NOT preserve 2εN bounds unless you track which stream contributed each observation. The original per-stream invariant breaks because inter-stream rank relationships are unknown. To merge properly, you need raw data or a hierarchical merge tree with propagated bounds—neither is "free."
Part 2: Centroid-Based Estimator
Core Idea: Cluster nearby values into weighted centroids. Use a scale function k(q) to control centroid granularity—small k(q) near tails means more centroids (higher accuracy), large k(q) near center means fewer centroids (less accuracy).
Structure: Each centroid stores (mean_value, weight). The scale function might be k(q) = δ/2 · q(1-q) for parameter δ, giving tighter clustering at extremes.
Updates:
- Add the new observation as weight-1 centroid
- Merge centroids that are close in cumulative weight space according to k(q)
- Limit total centroid count (e.g., 100-200)
Queries: Convert q to cumulative weight position q·total_weight, interpolate between the bounding centroids.
Complexity: O(δ) space for typical δ=100, O(log δ) or O(δ) update depending on merge strategy. Very fast queries with interpolation.
Guarantees: This is empirical, not worst-case. No adversarial input can be proven to stay within a fixed error bound. Testing on real data shows sub-1% error for middle quantiles and sub-0.1% for tails with δ=100, but pathological sequences (e.g., alternating 0,1,0,1,...) can break concentration.
Merging: Centroids from independent streams can be combined naturally—just merge-sort by value and recompress. The empirical behavior often remains good, but you still have no worst-case guarantee post-merge.
Comparison Table
| Criterion | Deterministic Summary | Centroid Method |
|---|---|---|
| Memory | O(1/ε) tuples | O(δ) centroids |
| Update | O(log(1/ε)) | O(log δ) to O(δ) |
| Guarantee | Worst-case ±εN rank | Empirical only |
| Duplicates | Handled naturally via rank | May cluster excessively |
| Skew | Uniform rank error | Can over-compress modes |
| Tail accuracy | Uniform across quantiles | Better near 0 and 1 |
| Mergeability | Breaks without tracking | Natural but still empirical |
Edge Cases and Practical Notes
-
Duplicates: Deterministic methods count each occurrence as a distinct rank position. Centroid methods may merge duplicates into one heavy centroid, reducing downstream resolution.
-
Adversarial order: Deterministic summaries honor the bound regardless of order. Centroid methods can degrade if values arrive in patterns that prevent effective clustering.
-
Exact extremes: Both should track exact min/max separately since these are trivial to maintain and critical for q=0, q=1.
-
Distributed systems: If you need to merge summaries from parallel workers, centroid methods are easier to implement but you lose formal guarantees. Deterministic summaries require either shipping raw samples or maintaining a merge tree with adjusted bounds—both add complexity.
Choosing an Approach
Use the deterministic summary when you need a provable guarantee, audit trails, or compliance requirements. Accept higher memory (though still sub-linear) and slightly more complex merge logic.
Use centroid methods for interactive dashboards, monitoring systems, and analytics where empirical accuracy suffices, tail precision matters, and you want fast updates with simple merging.