Kth Largest Element without a Heap
Updated
Problem statement
You are given an integer array nums and a rank k, starting at 1. Return the kth largest element. Count duplicate values separately, as if the array were sorted from largest to smallest.
Do not use a heap. For this exercise, implement a selection method without fully sorting the input, using the bounded portable interface below.
Function
kthLargest(nums: int[], k: int) → intExamples
Example 1
nums = [3,2,1,5,6,4]k = 2return = 5The two largest elements are 6 and 5.
Example 2
nums = [3,2,3,1,2,4,5,5,6]k = 4return = 4The descending sequence starts 6, 5, 5, 4; duplicates keep separate positions.
Constraints
1 <= nums.length <= 2000.1 <= k <= nums.length.-10^6 <= nums[i] <= 10^6.
More ByteDance Onsite Interview questions
- Search a Row- and Column-Sorted MatrixONSITE INTERVIEW · Seen Nov 2026
- Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Implement Multi-Head AttentionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dynamic Median with Additions and DeletionsONSITE INTERVIEW · Seen Oct 2026
- Serialize and Deserialize a String Key-Value StoreONSITE INTERVIEW · Seen Oct 2026
- Binary Tree Maximum Path SumONSITE INTERVIEW · Seen Oct 2026
- Merge Overlapping IntervalsONSITE INTERVIEW · Seen Oct 2026
- Tic-Tac-Toe Move WinnersONSITE INTERVIEW · Seen Oct 2026
- Least Recently Used CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Sustained Overspeed AlertsFleetx · ONSITE INTERVIEW
- Assign Escalations to TeamsAmazon · ONSITE INTERVIEW
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Spiral MatrixAthenahealth · ONSITE INTERVIEW
- Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
- Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Merge IntervalsApple · OA · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
ByteDance interview experiences
- ByteDance TikTok USDS SWE Intern Interview: 1 Experience
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience