Kth Largest Element in an Array
Updated
Problem statement
You are given an integer array nums and an integer k. Return the kth largest value in nums.
Repeated values each take their own place. Put all the values in order from largest to smallest, keeping repeats, and return the value in place k (counting from 1). For example, in [5, 5, 4] the 1st and 2nd largest values are both 5, and the 3rd largest is 4.
Follow-up from the interview: could the work be split across several threads? The judge does not test this; the editorial discusses it.
Function
findKthLargest(nums: int[], k: int) → intExamples
Example 1
nums = [7,2,9,4,9,1]k = 2return = 9From largest to smallest the values are 9, 9, 7, 4, 2, 1. The 2nd one is 9. Both 9s count, so the answer is not 7.
Example 2
nums = [3,-1,3,0]k = 4return = -1From largest to smallest the values are 3, 3, 0, -1. With k equal to the length, the answer is the smallest value, -1.
Example 3
nums = [-5,-2,-8,-2,-3]k = 3return = -3From largest to smallest the values are -2, -2, -3, -5, -8. The 3rd one is -3.
Constraints
1 <= k <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4
More Nutanix Phone Screen questions
- Swap Adjacent Linked List NodesPHONE SCREEN · Seen Mar 2025
- Top K Most Frequent ValuesPHONE SCREEN · Seen Mar 2025
- Unique Triplets Summing to ZeroPHONE SCREEN · Seen Sep 2024
- Find the Duplicate in a Read-Only ArrayPHONE SCREEN · Seen Apr 2021
- Kth Smallest Element in a BSTPHONE SCREEN · Seen Feb 2020
- Outline of a Binary TreePHONE SCREEN · Seen Jan 2020
- Print All File and Folder PathsPHONE SCREEN · Seen Dec 2019
- Inorder Predecessor in a BSTPHONE SCREEN · Seen Jan 2019
- K Largest and K Smallest ValuesPHONE SCREEN · Seen Jan 2019
- Odd Numbers Up and BackPHONE SCREEN · Seen Jan 2019
Similar questions at other companies
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
- Meeting Rooms IIeBay · PHONE SCREENeBay interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN
- Select the Top User MatchesDuolingo · PHONE SCREENDuolingo interview questions
- Meeting Rooms IIQuince · PHONE SCREENQuince interview questions
- Kth Largest Element in an ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Meeting Rooms IISecuronix · PHONE SCREEN
- Merge Multiple Sorted StreamsNVIDIA · PHONE SCREENNVIDIA interview questions
- Maximum Number of Events That Can Be AttendedSnowflake · PHONE SCREENSnowflake interview questions