Facebook Pixel

You are on level: Student

0%
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section
Expand section

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 per QUERY, in order. Each command is one line.
  • window_size is W, the most measurements the window may hold. Every measurement has an integer id and an integer value.
  • 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.
CommandWhat it doesPrints
PUSH id valueAdd a measurement, evicting the oldest if needednothing
QUERY pReport the p-th percentilethe value, or EMPTY
UPDATE id valueChange a measurement's value (Part 2)nothing
REMOVE idDelete 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 p and n are integers, ceil(p * n / 100) equals (p * n + 99) / 100 in 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 -2 and ranks 2 and 3 are both 8.

Part 1: Push, evict, and query

  1. PUSH id value adds a measurement as the newest one.
  2. If the window then holds more than W measurements, the oldest one is evicted.
  3. No PUSH uses an id that is currently in the window.
  4. QUERY p prints the p-th percentile of the measurements currently in the window, or EMPTY if 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.
  • W and the number of commands are between 1 and 2 × 105.

Tests 1–4 cover this part.

Part 2: Update a measurement
  1. UPDATE id value replaces the value of the measurement with that id and prints nothing.
  2. 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.
  3. An UPDATE of an id that is not in the window is ignored.

Tests 5–6 cover this part.

Part 3: Remove and reuse ids
  1. REMOVE id deletes the measurement with that id from the window and prints nothing. An id that is not in the window is ignored.
  2. A removed measurement no longer counts toward W.
  3. It must not affect later evictions: the next eviction removes the oldest measurement still in the window.
  4. 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.

Example
Input
4
7
PUSH 1 900
PUSH 2 1100
PUSH 3 950
PUSH 4 40000
PUSH 5 1000
QUERY 50
QUERY 99
Output
1000
40000
Explanation

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.