Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

K-th Smallest Latency Value

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is K-th Smallest Latency Value. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

TikTok Shop monitors server response times to detect latency regressions. Given an array of latency values and an integer k, return the k-th smallest latency, where k = 1 represents the smallest value.

Use an algorithm that avoids sorting the entire array when k is much smaller than the number of latency measurements.

Formal Specification

Implement kth_smallest_latency(latencies, k).

  • Input: latencies, a non-empty list of integers representing response times in milliseconds, and k, a 1-indexed integer.
  • Output: The integer latency that would appear at index k - 1 after sorting latencies in ascending order.
  • Duplicate latency values count as separate elements.

Constraints

  • 1 <= len(latencies) <= 10^5
  • 1 <= k <= len(latencies)
  • 0 <= latencies[i] <= 10^9
  • Duplicate latency values are allowed

Function Signature

def kth_smallest_latency(latencies, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output