The p95 of a batch of latencies
Dashboards quote p95 rather than the average, so one pathological request does not hide behind a thousand fast ones.
- Use the nearest-rank method: sort ascending, then take the value at position ceil(rank percent of the count), counting from 1.
- The rank is a percentage from 1 to 100; anything outside that gives 0.
- No readings gives 0.
PercentileValue(values: list<int>, rank: int) → int
C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
public int PercentileValue(List<int> values, int rank) {
}
Worked examples
| Call | Result |
|---|---|
PercentileValue(new List<int> { 1, 2, 3, 4, 5 }, 50) | 3 |
PercentileValue(new List<int> { 1, 2, 3, 4, 5 }, 100) | 5 |
PercentileValue(new List<int> { 1, 2, 3, 4, 5 }, 1) | 1 |
PercentileValue(new List<int> { 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 C#
public int PercentileValue(List<int> values, int rank) {
if (values.Count == 0 || rank < 1 || rank > 100) return 0;
var s = new List<int>(values);
s.Sort();
int pos = (rank * s.Count + 99) / 100;
return s[pos - 1];
}