Skip to the code
cf.
+25 XP

#214 Kth Slowest Page Load

medium

A performance report lists page load times in milliseconds in times, in no particular order. To flag outliers, it needs the k-th slowest load: the k-th largest value, counting repeats (so k = 1 is the slowest).

Write kthSlowest(times, k) that returns it. Try for average O(n) time, faster than sorting.

Example 1

Input: times = [320,150,980,410,150], k = 2
Output: 410

Slowest first: 980, 410, 320, 150, 150. The 2nd is 410.

Example 2

Input: times = [320,150,980,410,150], k = 5
Output: 150

The 5th slowest is the fastest load.

Constraints

  • 1 <= k <= 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).