Back to Practice
#0149
Kth Largest With a Min Heap
MediumDSA25 min20 XP
Problem
Given nums and k, return the kth largest value. Use a heap of size at most k.
Why This Matters
Top-k problems appear in rankings, recommendations, logs, and streaming analytics where sorting everything is wasteful.
Function Signature
def kth_largest(nums, k):
Examples
Example 1
Input[3, 2, 1, 5, 6, 4], k = 2
Output5
The two largest values are 6 and 5, so the 2nd largest is 5.
Constraints
- Return the exact requested value.
- Handle empty or small inputs when the prompt allows them.
- Keep the code readable and deterministic.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Input[3, 2, 1, 5, 6, 4], k = 2
Expected5
The two largest values are 6 and 5, so the 2nd largest is 5.
Hidden Test Categories
empty or smallest inputduplicates or repeated valueslarger realistic input