Drill

ProblemsPython › monitoring

The p95 of a batch of latencies

mediummonitoringPython

Dashboards quote p95 rather than the average, so one pathological request does not hide behind a thousand fast ones.

percentile_value(values: list<int>, rank: int) → int

Solve it in the editor →

Where you start

def percentile_value(values: list[int], rank: int) -> int:
    

Worked examples

CallResult
percentile_value([1, 2, 3, 4, 5], 50)3
percentile_value([1, 2, 3, 4, 5], 100)5
percentile_value([1, 2, 3, 4, 5], 1)1
percentile_value([10, 20, 30, 40, 50, 60, 70, 80, 90, 100], 95)100

Hint

In integers, ceil(rank * n / 100) is (rank * n + 99) / 100. Then subtract one for a zero-based index.

Reference solution in Python
def percentile_value(values: list[int], rank: int) -> int:
    if not values or rank < 1 or rank > 100:
        return 0
    s = sorted(values)
    pos = (rank * len(s) + 99) // 100
    return s[pos - 1]

The same problem in another language

More monitoring problems in Python