The problem. Given an integer array nums and an integer k, return the kth largest element — the one that would sit at position k if the array were sorted from largest to smallest. Duplicates count: in [3, 3, 1] the 2nd largest is 3. Can you do it without fully sorting?
Input: nums = [3, 2, 1, 5, 6, 4], k = 2
Output: 5
Input: nums = [3, 2, 3, 1, 2, 4, 5, 5, 6], k = 4
Output: 4 largest first: 6, 5, 5, 4, ...Sort the array; the kth largest is k places from the end.
function findKthLargest(nums, k) { const sorted = [...nums].sort((a, b) => a - b); return sorted[sorted.length - k]; // kth from the end}Time O(n log n), space up to O(n) depending on the sort. It does far more than asked: it puts every element in order when we only care about one position.
Walk the array while holding the k largest values seen so far in a min-heap (a structure that always hands you its smallest value in O(1), and adds or removes in O(log size)). When the heap grows past k, throw out its smallest value — it can't be among the top k. At the end, the heap holds exactly the k largest values, and its smallest one is the answer.
JavaScript has no built-in heap, so the JavaScript version includes a small one; the other languages use their standard priority queues.
function findKthLargest(nums, k) { const heap = []; // min-heap holding the k largest values seen so far const swap = (a, b) => ([heap[a], heap[b]] = [heap[b], heap[a]]); const push = (x) => { heap.push(x); for (let i = heap.length - 1; i > 0 && heap[(i - 1) >> 1] > heap[i]; i = (i - 1) >> 1) swap(i, (i - 1) >> 1); }; const popMin = () => { const last = heap.pop(); if (heap.length === 0) return; heap[0] = last; for (let i = 0; ; ) { const l = 2 * i + 1, r = l + 1; let small = i; if (l < heap.length && heap[l] < heap[small]) small = l; if (r < heap.length && heap[r] < heap[small]) small = r; if (small === i) break; swap(i, small); i = small; } }; for (const x of nums) { push(x); if (heap.length > k) popMin(); // drop the smallest: it can't be in the top k } return heap[0]; // the smallest of the k largest}Time O(n log k), space O(k). When k is small this is close to linear. But every element still pays a log k heap operation — can we get to plain O(n)?
Borrow quicksort's partition step — but only follow one side. Pick a pivot value and rearrange the current range into three zones: values bigger than the pivot, values equal to it, and values smaller (exactly the Dutch flag from Sort 0s, 1s and 2s).
Each round throws away everything outside one zone. With a random pivot, a round shrinks the range by about half on average, so the total work is about n + n/2 + n/4 + … ≈ 2n. The three-way split also keeps rounds fast when there are many equal values.
The simulator picks the middle element as the pivot so every run is repeatable; the code below picks a random one. Watch the three zones grow from both ends, and the search range shrink after each round.
Time O(n) on average, space O(1). The worst case is O(n²) — if every pivot were the worst possible value — but with a random pivot that is astronomically unlikely. It rearranges nums; copy the array first if the caller needs it unchanged.
O(n log n)Sorting orders all n elements, though only one position matters.
O(n)Up to O(n), depending on the language's sort.
O(n log k)In an interview, mention both: the heap is simple, predictable and great when k is small or the data arrives as a stream; quickselect is the fastest on average when the whole array is in memory.
[3, 2, 3, 1, 2, 4, 5, 5, 6] with k = 4 returns 4.[7, 7, 7, 7] returns 7 for any k; the equal zone ends the search in one round.[1] with k = 1 returns 1.Each of the n elements costs one heap push or pop of log k.
O(k)The heap never holds more than k values.
O(n) averageEach partition keeps only one side: n + n/2 + n/4 + … ≈ 2n. A run of bad pivots could reach O(n²), which a random pivot makes very unlikely.
O(1)Partitioning swaps in place in the input.