Back to Practice Dashboard
Top 150 InterviewEasy
Kth Largest Element In Array
Learn how to solve the 'Kth Largest Element In Array' problem. This detailed resource details brute force and optimized approaches.
Problem Statement
Easy
Given an integer array nums and an integer k, return the kth largest element in the array.
Note that it is the kth largest element in the sorted order, not the kth distinct element.
Can you solve it in O(n) complexity?
Write a function findKthLargest(nums: List[int], k: int) -> int.
Constraints
- •1 <= k <= len(nums) <= 10^5
- •-10^4 <= nums[i] <= 10^4
Examples
Example 1
Input
nums = [3,2,1,5,6,4], k = 2
Output
5
Explanation
The sorted array is [1,2,3,4,5,6]. The 2nd largest element is 5.
Example 2
Input
nums = [3,2,3,1,2,4,5,5,6], k = 4
Output
4
Explanation
The sorted array is [1,2,2,3,3,4,5,5,6]. The 4th largest element is 4.
Need a Hint?
Analyze the input constraints. Try sorting first (O(n log n)) or using a hash map/set to track seen elements in O(n) time.
Edge Cases to Watch
- Empty list or null input variables
- Single item lists/arrays
- Extremely large input bounds causing integer or stack overflow
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.