Approximate a percentile from buckets
GoogleData Scientist · Technical ScreenLast updated
This question tests the ability to interpolate values within histogram buckets to estimate a specific percentile, requiring an understanding of linear versus log-space assumptions.
Problem Statement
You are given a list of histogram buckets that represent the distribution of latency (in milliseconds) for a service. Each bucket is defined by a (lower_bound, upper_bound) pair and a count representing the number of observations within that interval.
Your task is to implement a function that estimates the p-th percentile (where 0 < p <= 1) of the distribution.
Assumptions
- Assume values are uniformly distributed within each bucket.
- The buckets cover the entire range of data; there are no observations outside the range defined by the minimum and maximum bounds.
- The function signature is:
estimate_percentile(buckets, p)wherebucketsis a list of objects containinglower,upper, andcount.
Example
Buckets: [{"lower": 0, "upper": 10, "count": 50}, {"lower": 10, "upper": 50, "count": 50}]
Goal: Find the 75th percentile.
- Total observations = 100.
- Target index = 0.75 * 100 = 75.
- The first bucket contains 50 observations. We need 25 more observations (75 - 50 = 25) from the second bucket.
- The second bucket has 50 observations over a range of 40 (50 - 10).
- Using uniform interpolation: 10 + (25 / 50) * 40 = 30.
Constraints
- The input
bucketsis sorted bylowerbound. - Handle cases where
pfalls exactly on a bucket boundary. - If a bucket has a
countof 0, it should not contribute to the percentile calculation. - What happens if the final bucket is open-ended (e.g.,
upper: Infinity)?
Hints
Hint 1
First, calculate the total count of observations to determine the target rank, which is `p * total_count`.
Hint 2
Iterate through the sorted buckets, maintaining a running sum of counts until the target rank falls within the current bucket.
Hint 3
Once the target bucket is identified, use the ratio of the remaining required count to the total bucket count to linearly interpolate between the bucket's lower and upper bounds.
Follow-up questions
- How would your approach change if the buckets represented logarithmic intervals rather than linear ones?
- If the buckets were extremely coarse (e.g., only two buckets), why might a uniform distribution assumption lead to significant error?
- How could you modify the data collection process or storage to provide more accurate quantile estimates without storing every raw data point (e.g., mention T-Digest or KLL sketches)?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.