Top 150 InterviewEasy

Kth Largest Element In Array

Detailed guide and Python implementation for the 'Kth Largest Element In Array' problem.

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?
Consider using Heap / Priority Queue-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.