Skip to the code
cf.
+25 XP

#394 Running Median Response Time

medium

An API dashboard updates its "median response time" label after every request. The median of a list is its middle value after sorting, or the average of the two middle values for an even count.

Write runningMedians(times) that returns an array where entry i is the median of times[0..i]. Answers within 1e-5 are accepted. Aim for O(log n) work per new time.

Example 1

Input: times = [5,2,8,1,9]
Output: [5,3.5,5,3.5,5]

[5] → 5, [2, 5] → 3.5, [2, 5, 8] → 5, [1, 2, 5, 8] → 3.5, [1, 2, 5, 8, 9] → 5.

Example 2

Input: times = [100]
Output: [100]

A single time is its own median.

Constraints

  • 1 <= times.length <= 5 * 10^4
  • 0 <= times[i] <= 10^5
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).