KatInterview

Streaming Quantile Estimation with Bounded Rank Error

Statistics & ExperimentsModel answer
HardModel answerStatistics & Experimentsstreaming-algorithmsquantilesapproximation

Last 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