Skip to contentKEEP THE CRAFTTHE SANCTUARY

Select the Kth Smallest Value

Partition around a pivot and keep only the side that holds the kth smallest value.

The problem

Implement kth_smallest(values, k). Return the kth smallest value in values, where k is 1-based (k = 1 means the minimum) and 1 <= k <= len(values). Equal values count as separate positions: in [7, 2, 5, 2, 9] the 1st and 2nd smallest are both 2 and the 3rd smallest is 5. Do not call sorted or list.sort, and leave values itself unchanged by working on a copy.

Starter code

def kth_smallest(values, k):
    pass

Public examples

  1. values = [7, 2, 5, 2, 9], k = 3gives5
  2. values = [3, 1, 4], k = 3gives4

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.