Find Top K Frequent Elements
Company: NVIDIA
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates a candidate's proficiency with frequency counting, selection techniques, use of appropriate data structures, and algorithmic time and space complexity analysis.
Constraints
- 1 <= len(nums) <= 200000
- -10^9 <= nums[i] <= 10^9
- 1 <= k <= number of distinct elements in nums
- Output length is exactly k
- Order: descending frequency; ties by ascending value
Hints
- Count occurrences with a hash map (e.g., collections.Counter).
- Sort distinct elements by (-frequency, value) and take the first k.
- Alternatively, use a heap of size k keyed by (frequency, -value) or bucket sort for linear-time selection.