Latency Percentile Tracker
MediumSorting · Netflix
You are building a latency monitor for a streaming service. Process the list operations in order. Each operation is one of:
["record", latency]: store one latency sample (a non-negative integer, in milliseconds). Returns nothing.["percentile", p]: report the nearest-rankp-th percentile of every sample recorded so far.
Nearest-rank percentile. Let n be the number of samples recorded so far and imagine them sorted in ascending order (duplicates kept). The rank is r = ceil(p * n / 100), and the answer is the r-th smallest sample (1-based). For example, with samples [10, 20, 30, 40], the 50th percentile has rank ceil(2) = 2 and is 20; the 51st percentile has rank ceil(2.04) = 3 and is 30. If no samples have been recorded yet, the answer is -1.
Return a list with one answer per "percentile" operation, in the order those operations appear.
Samples and queries are interleaved, so a query only sees the samples recorded before it. Aim for something better than re-sorting all samples on every query.
Example 1
- Input:
- operations = [["record", 120], ["record", 45], ["record", 300], ["record", 80], ["percentile", 50], ["percentile", 90], ["record", 20], ["percentile", 50]]
- Output:
- [80, 300, 80]
- Explanation
- After four samples the sorted list is
[45, 80, 120, 300]. p50 has rankceil(2.0) = 2, giving80; p90 has rankceil(3.6) = 4, giving300. Recording20makes five samples[20, 45, 80, 120, 300], and p50 now has rankceil(2.5) = 3, giving80again.
Example 2
- Input:
- operations = [["percentile", 99], ["record", 7], ["percentile", 1]]
- Output:
- [-1, 7]
- Explanation
- The first query sees no samples, so it returns
-1. After one sample, p1 has rankceil(0.01) = 1, which is that sample.
Example 3
- Input:
- operations = [["record", 5], ["record", 5], ["record", 5], ["record", 9], ["percentile", 75], ["percentile", 76]]
- Output:
- [5, 9]
- Explanation
- The sorted samples are
[5, 5, 5, 9]. p75 has rankceil(3.0) = 3, still a5; p76 has rankceil(3.04) = 4, which is9.
Constraints
- 1 ≤ operations.length ≤ 105
- 0 ≤ latency ≤ 109
- 1 ≤ p ≤ 100 (an integer)
- Each operation is
["record", latency]or["percentile", p].