Sliding Window Percentiles
Prerequisites: Latency Measurement and Percentiles
A latency monitor on the AAPL feed handler keeps only the most recent W measurements and reports any percentile of them on demand. Implement that window. It needs two orders over the same measurements:
- Arrival order decides which measurement leaves when the window is full.
- Value order decides which value sits at a given rank.
What to implement
process_commands(window_size, commands)returns one line perQUERY, in order. Each command is one line.window_sizeisW, the most measurements the window may hold. Every measurement has an integeridand an integervalue.- In the test files, the first line is
W, the second is the number of commands, and the commands follow one per line. - There are three parts, each adding a command on top of the previous ones. One solution must pass all ten tests.
| Command | What it does | Prints |
|---|---|---|
PUSH id value | Add a measurement, evicting the oldest if needed | nothing |
QUERY p | Report the p-th percentile | the value, or EMPTY |
UPDATE id value | Change a measurement's value (Part 2) | nothing |
REMOVE id | Delete a measurement (Part 3) | nothing |
Percentiles
Percentiles use the nearest-rank definition from the article. With n measurements in the window, sort their values in ascending order and number them from 1. The p-th percentile, for an integer p from 0 to 100, is the value at rank
rank = max(1, ceil(p * n / 100))
- Because
pandnare integers,ceil(p * n / 100)equals(p * n + 99) / 100in integer division, so no floating-point arithmetic is needed. - Percentile 0 gives rank 1, the minimum. Percentile 100 gives rank
n, the maximum. - Equal values occupy separate ranks: in the window
8, 8, -2, rank 1 is-2and ranks 2 and 3 are both8.
Part 1: Push, evict, and query
PUSH id valueadds a measurement as the newest one.- If the window then holds more than
Wmeasurements, the oldest one is evicted. - No
PUSHuses an id that is currently in the window. QUERY pprints thep-th percentile of the measurements currently in the window, orEMPTYif the window holds none.
Limits
- Values are usually latencies in nanoseconds, but the window accepts any signed 32-bit integer, negative ones included.
- Ids are signed 32-bit integers too.
Wand the number of commands are between 1 and 2 × 105.
Tests 1–4 cover this part.
Part 2: Update a measurement
UPDATE id valuereplaces the value of the measurement with that id and prints nothing.- The measurement keeps its age: it stays where it was in arrival order, so a later eviction still removes the oldest measurement by its original
PUSH. - An
UPDATEof an id that is not in the window is ignored.
Tests 5–6 cover this part.
Part 3: Remove and reuse ids
REMOVE iddeletes the measurement with that id from the window and prints nothing. An id that is not in the window is ignored.- A removed measurement no longer counts toward
W. - It must not affect later evictions: the next eviction removes the oldest measurement still in the window.
- An id may be pushed again after its measurement was removed or evicted. The new measurement is the newest one and has no connection to the old one.
Tests 7–10 cover this part. Test 9 also checks that an UPDATE or REMOVE of an id that is not in the window is ignored, on a window with one slow outlier.
4 7 PUSH 1 900 PUSH 2 1100 PUSH 3 950 PUSH 4 40000 PUSH 5 1000 QUERY 50 QUERY 99
1000 40000
The window holds four measurements, so the fifth PUSH evicts id 1, the oldest. The live values in ascending order are 950, 1000, 1100, 40000.
For p = 50, rank = max(1, ceil(50 * 4 / 100)) = 2, which is 1000. For p = 99, rank = ceil(396 / 100) = 4, which is the slow sample 40000.
The mean of the four, 10762.5, is not a latency any of them had.