Live Latency Median Ledger
Preview mode. Log in to edit, run, submit, and save progress.
Live Latency Median Ledger
The observability team at a streaming platform runs a live dashboard that watches request latency in real time. Latency readings arrive one at a time: the $i$-th reading is $A_i$ milliseconds, and there are $N$ readings in total. Averages on the dashboard kept getting wrecked by outlier spikes, so the team switched to medians. Every time a new reading lands, the dashboard immediately writes the current median of all readings seen so far into a ledger. When the number of readings seen so far, $k$, is odd, the median is the usual middle value - the $\frac{k+1}{2}$-th smallest. When $k$ is even, the dashboard records the lower median: the $\frac{k}{2}$-th smallest of the $k$ readings (ties broken by position in sorted order, so duplicates count separately). At the end of the day the team audits the ledger. Your job is to compute the sum of all $N$ recorded medians. ### Function Description Complete the function `medianLoadSum` provided in the editor. The function receives the following parameters:
| Parameter | Type | Description |
|---|---|---|
| $N$ | integer | the number of latency readings |
| $A$ | array of integers | the readings in arrival order; $A_i$ is the $i$-th reading |
The function must return a single integer: the sum of the median recorded after each of the $N$ readings. ### Input Format - The first line contains a single integer $N$. - The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$. ### Output Format Return a single integer: the sum of the $N$ recorded medians. ### Notes - Lower median rule: for an even count $k$, the recorded median is the $\frac{k}{2}$-th smallest reading. Mini example: after the readings $4, 9, 2, 7$ arrive ($k = 4$), the sorted multiset is $[2, 4, 7, 9]$ and the recorded median is the $2$-nd smallest, which is $4$ - not $7$ and not the average $5.5$. - For an odd count $k$ the median is simply the middle element of the sorted multiset, i.e. the $\frac{k+1}{2}$-th smallest. - Duplicated readings are counted with multiplicity: after $5, 5, 5, 5$ the recorded median is $5$ at every step. - The answer can reach $2 \times 10^{14}$, so use a 64-bit integer type for the running sum.
Examples
```text 5 3 1 4 1 5 ```
```text
Explanation: The ledger evolves reading by reading: - After $3$: seen $= [3]$, $k = 1$ (odd) $\rightarrow$ median $= 3$. - After $1$: seen $= [1, 3]$, $k = 2$ (even) $\rightarrow$ lower median $=$ $1$-st smallest $= 1$. - After $4$: seen $= [1, 3, 4]$, $k = 3$ (odd) $\rightarrow$ median $= 3$. - After $1$: seen $= [1, 1, 3, 4]$, $k = 4$ (even) $\rightarrow$ lower median $=$ $2$-nd smallest $= 1$. - After $5$: seen $= [1, 1, 3, 4, 5]$, $k = 5$ (odd) $\rightarrow$ median $= 3$. Sum of recorded medians $= 3 + 1 + 3 + 1 + 3 = 11$.
Approach hint
After each reading you need the current median of everything seen so far. Re-sorting from scratch each time works but costs far too much - think about what ordered information you can maintain incrementally.
Common mistake
Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.
```text
5
3 1 4 1 5
``````text