Technique
Quickselect
Partition around a pivot and recurse into the side containing index k, giving expected O(n) for the k-th smallest.
Learn firstTwo Pointers
0%
mastery
0/3 solved
0/3 solved
Recognise it
The signal
single k-th largest/smallest query on an array
1Brute force
Sort O(n log n).
2The observation
Sorting both sides: only the side containing k matters.
3Why it works
After partition, the pivot sits at its final sorted position.
Reach for it when
- +k closest points (unordered output)
Not when
- ×streaming input (heap)
- ×many queries (sort once)
- ×adversarial pivots without randomisation
Practice
0/3 solved
- Top K Frequent ElementsAmazon, MetaMedium
- Kth Largest Element in an ArrayMeta, AmazonMedium
- K Closest Points to OriginMeta, AmazonMedium
Pitfalls
Misconceptions
- !quickselect is worst-case linear (only with median-of-medians)