CodePark/ problems / Kth Largest Number
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Heaps and top-k

Kth Largest Number

mediumabout 20 minutes in an interview

Picture nums sorted from biggest to smallest, and count along to position k (the first position is 1). Return the number at that spot: the k-th largest.

  • Repeated numbers each take their own position. In [5, 5, 1], positions 1 and 2 are both 5.
  • k is at least 1 and at most nums.length.
  • nums can hold up to 100,000 numbers, and some can be negative.
  • You can reorder nums or work on a copy.
Input: nums = [7, 10, 4, 3, 20, 15], k = 3 Output: 10From largest to smallest: 20, 15, 10, …
Input: nums = [5, 5, 5, 1], k = 2 Output: 5