Computing Percentiles from Aggregated Histogram Data
Statistics & ExperimentsModel answerLast 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:
-
Finding the Target Bucket: Describe how to identify which bucket contains the desired percentile value.
-
Limitations of Naive Approaches: Explain why simply returning the center point of the identified bucket produces poor estimates in many scenarios.
-
Refined Estimation: Present a method for producing a better point estimate by making explicit assumptions about how values are distributed within a bucket.
-
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
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Estimating percentiles from bucketed data requires three key steps: locating the correct bucket through cumulative counting, choosing an interpolation strategy based on explicit assumptions, and acknowledging the uncertainty inherent in aggregated data.
Step 1: Locate the Containing Bucket
First, compute the total number of observations across all buckets. For the pth percentile, calculate the target rank as rank = (p/100) × total_count.
Traverse the buckets in order while maintaining a cumulative count. The percentile lies in the first bucket where the cumulative count meets or exceeds the target rank. If using cumulative sums, this can be found in O(K) time with a linear scan, or O(log K) with binary search over precomputed prefix sums.
Example: With 1000 total observations and p=75, the target rank is 750. If buckets have cumulative counts [200, 500, 850, 1000], the 75th percentile falls in the third bucket (cumulative count 850).
Step 2: Why Midpoint Fails
Simply returning the bucket's midpoint (lower_bound + upper_bound) / 2 ignores critical information: where the target rank sits within the bucket's count range.
Consider a bucket [100, 200] with 500 observations. If the target rank is at position 10 out of 500 in this bucket, the percentile should be near 100, not 150. The midpoint assumption only works when the target rank is exactly centered in the bucket's count.
This problem worsens with:
- Wide buckets: A [0, 1000] bucket gives 500 as the midpoint regardless of whether you want the 10th or 90th percentile
- Skewed distributions: In heavy-tailed data, values cluster near one boundary
- Logarithmic spacing: Buckets like [1, 10], [10, 100], [100, 1000] represent vastly different value ranges
Step 3: Interpolation Under Assumptions
A better approach uses the fractional position within the bucket:
cumulative_before = cumulative count of all previous buckets
position_in_bucket = (target_rank - cumulative_before) / current_bucket_count
Linear Interpolation (assumes uniform distribution within bucket):
estimate = lower_bound + position_in_bucket × (upper_bound - lower_bound)
This treats values as evenly spread across the bucket's range. It's appropriate for linearly-spaced buckets measuring naturally linear quantities.
Logarithmic Interpolation (for multiplicative data): When buckets are log-spaced (e.g., [1, 10], [10, 100]) and measuring quantities that vary multiplicatively (latencies, file sizes, network traffic), interpolate in log-space:
log_lower = log(lower_bound)
log_upper = log(upper_bound)
log_estimate = log_lower + position_in_bucket × (log_upper - log_lower)
estimate = exp(log_estimate)
This maintains the geometric structure of the data. For example, in bucket [10, 100] at position 0.5, linear interpolation gives 55, while log interpolation gives ~31.6 (the geometric mean), which better reflects the multiplicative nature.
State Your Assumption Explicitly: Any point estimate beyond "the value is in this bucket" requires assuming a within-bucket distribution. Make this explicit: "Under the assumption of uniform distribution within the bucket, the estimate is X."
Step 4: Edge Cases and Complexity
Percentile at Bucket Boundaries: If the target rank lands exactly at a bucket boundary, return that boundary value directly.
Extreme Percentiles:
- For the 0th percentile, return the lower bound of the first non-empty bucket
- For the 100th percentile, return the upper bound of the last non-empty bucket
Empty Buckets: Skip them when computing cumulative counts. They contribute zero to the total.
Coarse Granularity: When buckets are very wide relative to the data range, report an uncertainty interval rather than claiming false precision. For instance: "The 90th percentile lies in [1000, 5000]; under uniform assumption, approximately 4200."
Discrete vs. Continuous: For discrete integer data (e.g., query counts), the interpolated value should be rounded. For continuous measurements (e.g., latency), maintain decimal precision.
Computational Efficiency:
- Simple scan: O(K) where K is the number of buckets
- With precomputed cumulative sums: O(log K) using binary search
- Space: O(1) for scan, O(K) for precomputed arrays
Example Calculation
Buckets: [0,10,40], [10,20,60], [20,30,80], [30,40,20] Total: 200 observations Find: 70th percentile
Target rank = 0.70 × 200 = 140
Cumulative counts: 40, 100, 180, 200 The 140th value falls in bucket [20, 30] with 80 observations.
Position in bucket: (140 - 100) / 80 = 0.5 Estimate: 20 + 0.5 × (30 - 20) = 25
Common Mistakes
- Forgetting to handle empty buckets in cumulative sums
- Using linear interpolation on log-spaced buckets
- Reporting excessive precision from coarse buckets
- Ignoring the difference between rank calculation for inclusive vs. exclusive boundaries
- Not stating the distributional assumption when giving a point estimate
A strong answer acknowledges that bucketed data fundamentally limits precision and that any point estimate rests on an assumption that may not hold in reality.