TikTok Interview Coding Question: Kth Largest Element in an Array

19 Views
No Comments

Given an integer array nums and an integer k, return the k-th largest element in the array.

Note that it is the k-th largest element in the sorted order, not the k-th distinct element.

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
function findKthLargest = (nums, k) {// ...}

This problem asks for the k-th largest element in an integer array, counting duplicates by sorted position rather than distinct value. A common solution is Quickselect for average O(n) time, or a min-heap of size k to track the top k elements. In the examples, the 2nd largest in [3,2,1,5,6,4] is 5, and the 4th largest in [3,2,3,1,2,4,5,5,6] is 4.

END
 0