Kth Largest Element in an Array
Find kth largest element in array Example: [3,2,1,5,6,4], k=2 => 5
I never sort the whole array. A size-k heap keeps the biggest k, or one quickselect partition lands straight on the answer.
Examples
You write
find_kth_largest(nums: List[int], k: int)
Example 1
- Input
- nums = [3,2,1,5,6,4], k = 2
- returns
- 5
Example 2
- Input
- nums = [3,2,3,1,2,4,5,5,6], k = 4
- returns
- 4
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsHeaps
I reach for it when
- You need the k largest or k smallest items.
- You repeatedly take the best remaining item.
Say this before you type. Say what the heap holds, how big it stays, and what sits on top.
All 22 Heaps problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Prep guides that list this problem
Our company tags come from public interview write-ups. None of these is a confirmed question.