K-th Largest Element
Two common approaches are a min-heap of size k and Quickselect. The heap approach maintains the k largest elements seen so far and keeps the smallest of them at the root, giving O(n log k) time. Quickselect partitions around a pivot and has O(n) expected time with O(n²) worst case.
Min-heap of size k: O(n log k) time, O(k) space.
Quickselect: O(n) expected time.
Quickselect worst case: O(n²).
For small k, the heap is often simple and efficient.
Quickselect is attractive when average linear-time selection is desired.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience