Return the Most Frequent Values in an Array
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Given a non-empty integer array, return the `k` values that appear most frequently. Ties are broken by smaller value first.
Implement:
```python
def top_k_frequent(nums: list[int], k: int) -> list[int]:
pass
```
Quick Answer: Practice a Google coding interview problem focused on return the most frequent values in an array. The prompt emphasizes edge cases, clean implementation, and verifiable test behavior without revealing the solution.
Return k values with highest frequency, breaking ties by smaller value.
Examples
Input: {"nums":[1,1,1,2,2,3],"k":2}
Expected Output: [1,2]
Explanation: Common sample.
Input: {"nums":[1],"k":1}
Expected Output: [1]
Explanation: Single.