AlgoMaster Logo
AlgoMasterCalculate Nearest-Rank Latency Percentilesmedium

Calculate Nearest-Rank Latency Percentiles

medium

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:

  1. Sort the latency samples in ascending order.
  2. Compute the 1-based rank ceil(p * n / 100), where n is the number of samples.
  3. 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^5
  • 0 <= 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.
Hints

Loading...
CallReturns
new NearestRankPercentileCalculator()null
latencyPercentiles([10,20,30,40,50,60,70,80,90,100])[50,100,100]

For 10 samples, the nearest ranks are 5, 10, and 10, which select 50, 100, and 100.

Run checks these cases. Submit also runs a larger hidden set.