PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's mastery of finding the k-th largest element using selection algorithms and heap data structures, along with algorithmic time and space complexity analysis and techniques for adapting solutions to streaming inputs.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Find the kth largest element

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given an unsorted array of n integers and an integer k (1 ≤ k ≤ n), return the k-th largest element without fully sorting the array. Design two methods: ( 1) an expected linear-time selection algorithm and ( 2) a heap-based approach. For each, describe the algorithm, analyze time and space complexity, and discuss how you would adapt it to find the k-th largest element in a data stream.

Quick Answer: This question evaluates a candidate's mastery of finding the k-th largest element using selection algorithms and heap data structures, along with algorithmic time and space complexity analysis and techniques for adapting solutions to streaming inputs.

Given an unsorted array of n integers `nums` and an integer `k` (1 ≤ k ≤ n), return the k-th largest element in the array. Note this is the k-th largest element in sorted order, not the k-th distinct element. You should NOT fully sort the array. Two classic approaches solve this in better-than-O(n log n) terms: 1. **Heap-based (implemented here):** maintain a min-heap of size k while scanning the array. After processing every element, the heap holds the k largest values seen so far, and its smallest element (the root) is the k-th largest. Runs in O(n log k) time and O(k) space, and adapts naturally to a streaming setting — just push each new arriving value and pop when the heap exceeds size k; the root is always the running k-th largest. 2. **Quickselect (expected linear time):** partition around a (randomized) pivot and recurse only into the side that contains the target rank. Expected O(n) time, O(1) extra space, but does not extend cleanly to an unbounded stream. Return the value of the k-th largest element.

Constraints

  • 1 ≤ k ≤ n ≤ 10^5
  • -10^4 ≤ nums[i] ≤ 10^4
  • The answer is the k-th largest in sorted order, counting duplicates (not the k-th distinct value).

Examples

Input: ([3, 2, 1, 5, 6, 4], 2)

Expected Output: 5

Explanation: Sorted descending: [6, 5, 4, 3, 2, 1]. The 2nd largest is 5.

Input: ([3, 2, 3, 1, 2, 4, 5, 5, 6], 4)

Expected Output: 4

Explanation: Sorted descending: [6, 5, 5, 4, 3, 3, 2, 2, 1]. The 4th largest is 4 (duplicates count).

Input: ([1], 1)

Expected Output: 1

Explanation: Single element; the 1st largest is itself.

Input: ([7, 7, 7, 7], 2)

Expected Output: 7

Explanation: All elements equal; any rank is 7 (duplicates are counted separately).

Input: ([-1, -2, -3, -4, -5], 1)

Expected Output: -1

Explanation: Largest value of all-negative array is -1.

Input: ([-1, -2, -3, -4, -5], 5)

Expected Output: -5

Explanation: k = n boundary: the 5th largest (i.e. the smallest) is -5.

Input: ([2, 1], 2)

Expected Output: 1

Explanation: Sorted descending: [2, 1]. The 2nd largest is 1.

Hints

  1. You do not need to fully sort the array. To find the k-th largest, you only need to track the k largest elements seen so far.
  2. A min-heap of fixed size k is ideal: its smallest element (the root) is exactly the k-th largest once all elements are processed. Pop whenever the heap grows beyond k.
  3. For an expected linear-time approach instead, use Quickselect: randomly pick a pivot, partition, and recurse only into the partition that contains rank n-k.
  4. Streaming adaptation: the min-heap approach works online — push each arriving value, pop when size exceeds k, and the root is always the running k-th largest.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)