Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Kth Minimum in Array

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

Your question is Kth Minimum in Array. 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

Simpl analyzes arrays of payment amounts to identify low-value transactions efficiently. Given an unsorted array of integers, return the kth smallest element, counting duplicate values as separate elements.

Use 1-based indexing for k. The input is guaranteed to contain at least k elements. The function may use additional memory, but should avoid sorting the entire array when a more targeted approach is possible.

Formal Specification

Implement kth_smallest(nums, k):

  • Input: nums, a non-empty list of integers, and k, a positive integer.
  • Output: The integer that would appear at index k - 1 if nums were sorted in nondecreasing order.
  • The original array does not need to be preserved if your approach modifies it.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= k <= nums.length
  • -10^9 <= nums[i] <= 10^9
  • Duplicate values count as separate elements

Function Signature

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