← All mock interviewsHints used: 0
Kth Largest Element in an Array Mock Interview
- ✓Problem→
- 2Clarifying Questions→
- 3Constraints→
- 4Brute Force→
- 5Complexity Analysis→
- 6Pattern Recognition→
- 7Optimized Solution→
- 8Implementation→
- 9Testing→
- 10Follow-Up→
- 11Evaluation
Problem
Given an integer array nums and an integer k, return the k-th largest element in the array in sorted order (not the k-th distinct element). For example, in [3,2,1,5,6,4] with k = 2 the answer is 5. Try to do better than sorting the whole array.
Constraints
- 1 ≤ k ≤ n ≤ 10^5
- -10^4 ≤ nums[i] ≤ 10^4
Example
in: nums = [3,2,1,5,6,4], k = 2
out: 5
Clarify
Before choosing anything: what would you ask the interviewer? What assumptions are you making? (Duplicates? Empty input? Value ranges? What to return when there is no answer?)