🚀 Phases 1–4 are live — Days 1–13 + Day 17 are fully written. See the roadmap →

Kth Largest Element in an Array easy

Description

Given an integer array nums and an integer k, return the k-th largest element in the array. Note that it’s the k-th largest in sorted order, not the k-th distinct.

Examples

> Case 1:
    Input:  nums = [3, 2, 1, 5, 6, 4], k = 2
    Output: 5
 
> Case 2:
    Input:  nums = [3, 2, 3, 1, 2, 4, 5, 5, 6], k = 4
    Output: 4

Constraints

  • 1 <= k <= nums.length <= 10^5

Two heap approaches — pick your trade-off

Approach 1: Sort and pick. Sort the array, return nums[n - k]. Time O(n log n), space O(1) (or O(n) depending on the sort). Easy but wasteful — we sorted everything when we only needed the k-th largest.

Approach 2: Max-heap of all elements. Heapify all n elements (O(n)), then pop k times. Time O(n + k log n). Better when k is small.

Approach 3 (the classic trick): Size-K min-heap. Walk the array once, maintaining a heap that contains only the K largest elements seen so far. When the heap exceeds size K, pop the smallest. After processing, the root is the K-th largest. Time O(n log k), space O(k) — best when k << n.

We’ll implement approach 3 — it’s the one interviewers want and it generalizes to streams of unknown length.

Why a min-heap for the K largest?

This trips everyone up the first time. The min-heap holds the K largest values we’ve seen so far, with the smallest of those at the top so we can quickly check “is the new value bigger than my current threshold?”

  • If new > heap.top() → the new value is bigger than the smallest of our K-best, so it deserves to be in the top K. Push it. If the heap now has K+1 elements, pop the smallest.
  • If new <= heap.top() → the new value isn’t even bigger than our current K-th largest, so it definitely isn’t in the top K. Skip it.

At the end, the heap holds the K largest values, and the root is the smallest of them — which is the K-th largest overall.

Code

Why this is the interview answer

Three reasons it’s better than just sorting:

  1. It works on streams. If nums is an infinite generator, you can’t sort it — but the size-K heap approach keeps memory bounded and gives you the answer at every moment.
  2. Space is O(k), not O(n). For massive n and small k (top 10 search results out of billions), this matters.
  3. It generalizes immediately to Top K Frequent, K Closest Points, K Most Similar Strings, etc. — same template, different priority.

Quickselect gives an even faster O(n) average solution using a partition-based approach (no heap at all). It’s better for fixed-size arrays in memory but harder to code under interview pressure and has worst-case O(n²). The heap solution is the safer choice unless you’re explicitly asked for it.

Analysis

ApproachTimeSpaceWhen to use
SortO(n log n)O(1)Small n, or k close to n
Heapify all + pop kO(n + k log n)O(n)When you already have the array
Size-K min-heapO(n log k)O(k)Streams, large n, small k
QuickselectO(n) avg, O(n²) worstO(1)When you can afford the complexity