Kth Largest Element in an Array
Updated
Problem statement
Given an integer array nums and an integer k, return the k-th largest element of the array.
This is the element in position k when the array is sorted from largest to smallest, so equal values count separately. It is not the k-th largest distinct value.
Function
findKthLargest(nums: int[], k: int) → intExamples
Example 1
nums = [3,1,5,2,4]k = 2return = 4From largest to smallest the values are 5, 4, 3, 2, 1. The 2nd largest is 4.
Example 2
nums = [4,4,1]k = 2return = 4From largest to smallest: 4, 4, 1. Equal values count separately, so the 2nd largest is the second 4.
Example 3
nums = [7]k = 1return = 7With one element, the 1st largest is that element.
Constraints
1 <= k <= nums.length <= 10^4-10^4 <= nums[i] <= 10^4
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
- Top K Largest Values from a StreamThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Generate Complete Inputs for a Supplied ClassifierGeneral Motors · ONSITE INTERVIEW
- Minimum Meeting Rooms Without a Sweep LineGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Dominant Index with Strict Doubling GapsGoogle · ONSITE INTERVIEWGoogle interview questions
- Top K Largest Values with a HeapMeta · ONSITE INTERVIEWMeta interview questions
- Top K Frequent ElementsCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Select K Paired Indices for Maximum ScoreAmazon · ONSITE INTERVIEW
- Kth Largest Element in an ArrayOneTrust · ONSITE INTERVIEW