KatInterview

Computing Percentiles from Aggregated Histogram Data

Statistics & ExperimentsModel answer
MediumModel answerStatistics & Experimentsstatisticspercentileshistograms

Last updated

Given histogram buckets with counts, estimate a percentile value by finding the containing bucket and interpolating within it under distributional assumptions.

You have aggregated frequency data represented as a histogram. Each bucket is defined by three values: a lower boundary, an upper boundary, and the count of observations that fall within that range.

Your task is to estimate a specific percentile of the underlying data distribution using only this bucketed information—the individual raw observations are not available.

Input Format:

  • A collection of buckets, each containing (lower_bound, upper_bound, count)
  • Buckets do not overlap and are ordered by their boundaries
  • A target percentile value (e.g., 50 for median, 95 for 95th percentile)

Your Analysis Should Address:

  1. Finding the Target Bucket: Describe how to identify which bucket contains the desired percentile value.

  2. Limitations of Naive Approaches: Explain why simply returning the center point of the identified bucket produces poor estimates in many scenarios.

  3. Refined Estimation: Present a method for producing a better point estimate by making explicit assumptions about how values are distributed within a bucket.

  4. Practical Considerations: Discuss how to handle:

    • Edge cases (extreme percentiles, empty buckets, boundaries)
    • Different bucket spacing patterns (uniform vs. logarithmic)
    • Uncertainty quantification when granularity is coarse
    • Computational efficiency for large bucket counts

Questions to Consider:

  • Should buckets be treated as inclusive on the left and exclusive on the right?
  • Are the underlying values continuous or discrete?
  • What distributional assumption is most reasonable within a bucket?
  • When should you report an interval rather than a point estimate?

Hints

Hint 1

Calculate cumulative counts to determine which bucket contains your target rank

Hint 2

Linear interpolation assumes uniform distribution within a bucket—state this assumption explicitly

Hint 3

For logarithmically-spaced buckets, consider interpolating in log-space rather than linear space