← All mock interviewsHints used: 0
Top K Frequent Elements 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 most frequent elements. The answer is guaranteed to be unique (no ties at the k-th position), and it may be returned in any order. Aim for a solution better than O(n log n).
Constraints
- 1 ≤ n ≤ 10^5
- -10^4 ≤ nums[i] ≤ 10^4
- 1 ≤ k ≤ number of distinct elements
Example
in: nums = [1,1,1,2,2,3], k = 2
out: [1,2]
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?)