Find the K-th Largest Element Without Using a Heap
Heaps, Sorting & SearchAuto-graded · 21 test casesLast updated
Tests implementation of quickselect to find the k-th largest value in an unsorted array, treating duplicates as separate elements.
You're analyzing a stream of numerical measurements that arrive in arbitrary order. Write a function that identifies the k-th largest measurement from the collection.
Your function receives a list of integer measurements and a position k, then returns the k-th largest value. Duplicate measurements are treated as distinct entries—if three readings all show 100, they occupy three separate positions when ranked from largest to smallest.
Important constraint: You may not use any heap or priority queue data structure (including Python's heapq module) in your solution.
Function Signature
def find_kth_largest(nums: list[int], k: int) -> int:
Parameters
nums: A list of integer measurements in the order they were recordedk: The position to find (1-indexed), where k=1 means the largest value, k=2 means the second-largest, etc.
Return Value
The integer value at the k-th position when all measurements are ranked from largest to smallest.
Constraints
1 <= k <= len(nums) <= 10^5-10^9 <= nums[i] <= 10^9kis guaranteed to be valid (never exceeds the length ofnums)
Examples
Example 1
Input: find_kth_largest([70, 20, 90, 40, 90, 10], 2)
Output: 90
Example 2
Input: find_kth_largest([70, 20, 90, 40, 90, 10], 4)
Output: 40
Example 3
Input: find_kth_largest([-50, 0, -50, 30], 3)
Output: -50
Hints
Hint 1
Consider selection algorithms that find a specific rank without fully sorting the entire array
Hint 2
A partition-based approach can eliminate portions of the array that definitely don't contain your target rank
Hint 3
When many duplicate values exist, a three-way partition (less than, equal to, greater than) avoids redundant work compared to a two-way split
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The straightforward solution is to sort the entire array in descending order and return the element at index k-1. This directly addresses the problem: duplicates are preserved as separate entries, and retrieving the k-th position is a simple array access.
While sorting requires O(n log n) time, it guarantees correctness for all edge cases: when k equals 1 (the maximum), when k equals the array length (the minimum), when all values are identical, and when the array contains many duplicates.
A more sophisticated approach would use the quickselect algorithm, which partitions the array around a pivot and recursively searches only the partition containing the target rank. With random pivot selection and three-way partitioning (to handle duplicates efficiently), quickselect achieves O(n) expected time. However, the implementation is more complex and requires careful handling of edge cases.
For interview purposes, the sorting solution demonstrates clarity and correctness. If the interviewer requests optimization, you can then discuss quickselect as a follow-up.
Complexity
- Time: O(n log n) where n is the length of the input array, dominated by the sorting step
- Space: O(n) for Python's Timsort implementation (or O(1) if the sort is done in-place, though the reference solution creates a sorted copy)
Solution Code
def find_kth_largest(nums: list[int], k: int) -> int:
# Sorting preserves duplicate entries, so equal values count separately.
return sorted(nums, reverse=True)[k - 1]
This solution handles all specified constraints: it works with negative numbers, treats duplicates as separate elements, and correctly returns the k-th largest value for any valid k within the array bounds.