Given a list of positive integer latencies, an integer num_buckets and an integer bucket_width, return a list of length num_buckets whose i-th element is the count of latencies falling in the range [i * bucket_width, (i + 1) * bucket_width).
Latencies that fall outside all buckets (values greater than or equal to num_buckets * bucket_width) are ignored.
Function Signature
def latency_buckets(latencies: list[int], num_buckets: int, bucket_width: int) -> list[int]:
...
Rules
-
Each bucket includes its lower edge and excludes its upper edge, so a latency exactly equal to
k * bucket_width
is counted in bucket
k
.
-
The output always has exactly
num_buckets
entries, including buckets with a count of 0.
Constraints
-
1 <= len(latencies) <= 10^5
-
1 <= latencies[i] <= 10^9
-
1 <= num_buckets <= 10^4
-
1 <= bucket_width <= 10^6
Examples
Input: latencies = [10,25,33,47,52,78,99,150,200], num_buckets = 5, bucket_width = 20
Output: [1,2,2,1,1]
The buckets are [0,20), [20,40), [40,60), [60,80) and [80,100). 10 lands in bucket 0, 25 and 33 in bucket 1, 47 and 52 in bucket 2, 78 in bucket 3 and 99 in bucket 4, while 150 and 200 are at least 100 and are ignored.
Input: latencies = [5,5,5,15,25,35], num_buckets = 3, bucket_width = 10
Output: [3,1,1]
The buckets are [0,10), [10,20) and [20,30). The three 5s go to bucket 0, 15 to bucket 1 and 25 to bucket 2. 35 is at least 30, so it is ignored.
Input: latencies = [20,40,19,60], num_buckets = 3, bucket_width = 20
Output: [1,1,1]
19 is in [0,20), while 20 and 40 are the lower edges of buckets 1 and 2 and count there. 60 equals 3 * 20, the first value past the last bucket, so it is ignored.