An average latency can look healthy while a small group of requests remains very slow. Percentiles expose that tail by reporting the latency at or below which a chosen percentage of samples falls.
Design a NearestRankPercentileCalculator class:
NearestRankPercentileCalculator() creates a stateless calculator.int[] latencyPercentiles(int[] latencies) returns [p50, p95, p99] using the nearest-rank method.
To calculate percentile p:
- Sort the latency samples in ascending order.
- Compute the 1-based rank
ceil(p * n / 100), where n is the number of samples. - Return the sample at zero-based index
rank - 1.
Nearest rank always selects an observed latency; it does not interpolate between samples. Do not mutate the input array, and treat each method call independently.
Example 1:
Input:
Output:
Explanation: The p50 rank is ceil(50 * 10 / 100) = 5, so p50 is 50. The p95 and p99 ranks both round up to 10, so both values are 100.
Example 2:
Input:
Output:
Explanation: With four samples, p50 selects rank 2. Both p95 and p99 select rank 4, the maximum sample.
Constraints
1 <= latencies.length <= 10^50 <= latencies[i] <= 10^9- Use 1-based nearest rank without interpolation.
- Return values in the order
[p50, p95, p99]. - Do not mutate
latencies. - At most
100 calls are made to latencyPercentiles.