← All Exercises
Quickselect for the Kth Smallest Element
intermediatetypescript
Background
Quickselect powers order statistics, percentile calculations, and top-k queries with better constants than heap-based selection.
Problem
Given an unsorted array and a 1-based rank k, return the kth smallest element using quickselect instead of a full sort.
Input
An array of integers and an integer k where 1 <= k <= array length.
Output
The kth smallest value, counting duplicates as separate positions.
Sample tests
Sample 1
Input: nums = [3,2,1,5,6,4], k = 2
Expected: 2
Sample 2
Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Expected: 3
Sample 3
Input: nums = [7,7,7,7], k = 3
Expected: 7
Your solution
Learning objective
Implement Dutch national flag partitioning and bound the search to one side of the pivot.