Skip to main content

74. Kth Largest Element in an Array

mediumAsked at Reddit

Find the kth-largest element. Reddit uses this to test heap and quickselect — the same primitive that powers their top-k post selector for ranking feed pages.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit ranking-team on-site question.
  • Blind (2025-11)Reported as Reddit feed-ranking canonical.

Problem

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 without sorting?

Constraints

  • 1 <= k <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4

Examples

Example 1

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

Example 2

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

Approaches

1. Sort + index

Sort descending; return nums[k-1].

Time
O(n log n)
Space
O(1)
function findKthLargest(nums, k) {
  return nums.sort((a, b) => b - a)[k - 1];
}

Tradeoff: Simple but doesn't exploit that we only need the kth element.

2. Min-heap of size k (optimal for streaming)

Keep a min-heap of size k. Push each element; pop when size > k. Top of heap is kth largest.

Time
O(n log k)
Space
O(k)
// Use a real heap library in production; sketch with sorted insert:
function findKthLargest(nums, k) {
  const heap = [];
  for (const n of nums) {
    heap.push(n);
    heap.sort((a, b) => a - b);
    if (heap.length > k) heap.shift();
  }
  return heap[0];
}

Tradeoff: Sublinear in k when k << n. Use a real binary heap for O(log k) push/pop.

Reddit-specific tips

Reddit interviewers grade on heap vs. quickselect trade-off. Bonus signal: discuss quickselect — O(n) average, O(n^2) worst-case — and partition-style algorithm. Connect to streaming top-k for hot post selection.

Common mistakes

  • Using max-heap (wrong for finding kth-largest; we want min-heap of size k).
  • Forgetting to pop when size > k.
  • Returning the top with sort().pop() — works but quadratic with the in-loop sort.

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • Top K Frequent Elements (LC 347) — same heap pattern.
  • Kth smallest in BST (LC 230).
  • Quickselect implementation.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why min-heap of size k?

The heap holds the k largest seen so far; the smallest among them is the kth largest.

When to prefer quickselect?

Batch (offline) data, where you can mutate the input. Average O(n) beats heap's O(n log k).

Practice these live with InterviewChamp.AI

Drill Kth Largest Element in an Array and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →