Skip to the code
cf.
+25 XP

#215 Median Response Time

medium

An API dashboard shows the median response time of the last batch of requests. times is unsorted. If the count is odd, the median is the middle value in sorted order; if it is even, it is the average of the two middle values.

Write medianTime(times) that returns the median. Answers within 1e-5 count as correct. Aim for average O(n) time.

Example 1

Input: times = [120,80,200,95,110]
Output: 110

Sorted: 80, 95, 110, 120, 200. The middle is 110.

Example 2

Input: times = [40,10,30,20]
Output: 25

Sorted: 10, 20, 30, 40. The middle two average to 25.

Constraints

  • 1 <= times.length <= 100000
  • 0 <= times[i] <= 10^6
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).