Return the k most frequent values in an array with a deterministic tie rule
Company: Microsoft
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Given an integer array `nums` and an integer `k`, return the `k` values that occur most often in `nums`.
Rank the distinct values of `nums` by frequency (number of occurrences), highest first, and break ties between values with equal frequency by putting the smaller value first. Return the first `k` values of this ranking, in ranking order.
### Function Signature
```python
def k_most_frequent(nums: list[int], k: int) -> list[int]:
```
### Rules
- Each distinct value appears at most once in the output.
- The output order is the ranking order: frequency descending, then value ascending.
- The same rule decides which values are included when several values tie at the cutoff: a smaller value always ranks ahead of a larger value with the same frequency.
### Constraints
- `1 <= len(nums) <= 10^5`
- `-10^9 <= nums[i] <= 10^9`
- `1 <= k <= d`, where `d` is the number of distinct values in `nums`
### Examples
**Example 1**
```text
Input: nums = [4, 1, 4, 2, 1, 4, 3], k = 2
Output: [4, 1]
```
`4` occurs three times, `1` twice, and `2` and `3` once each.
**Example 2**
```text
Input: nums = [3, 3, 2, 2, 1, 1, 0], k = 2
Output: [1, 2]
```
`1`, `2` and `3` each occur twice. The tie is broken by the smaller value, so `1` and `2` are returned and `3` is not.
**Example 3**
```text
Input: nums = [-5, 7, -5, 7, 9], k = 3
Output: [-5, 7, 9]
```
`-5` and `7` each occur twice, and `-5` is smaller. `9` occurs once.
Overview: Return the k values that occur most often in an integer array, ranked by frequency with ties broken by the smaller value. It tests frequency counting, selecting the top k efficiently and producing a deterministic order when several values tie at the cutoff.
Read the full Microsoft Machine Learning Engineer interview experience this question came from
Given an integer array `nums` and an integer `k`, return the `k` values that occur most often in `nums`.
Rank the distinct values of `nums` by frequency (the number of times the value occurs), highest frequency first. When two values have the same frequency, the smaller value ranks first. Return the first `k` values of this ranking as a list, in ranking order.
Implement `k_most_frequent(nums, k)`.
### Rules
- Each distinct value appears at most once in the output.
- The output order is the ranking order: frequency descending, then value ascending.
- The same rule decides which values are included when several values tie at the cutoff: a smaller value always ranks ahead of a larger value with the same frequency.
### Constraints
- `1 <= len(nums) <= 10^5`
- `-10^9 <= nums[i] <= 10^9`
- `1 <= k <= d`, where `d` is the number of distinct values in `nums`
Every value and every frequency fits in a signed 32-bit integer. The Java and C++ signatures take and return 64-bit integers (`long` / `long long`).
### Examples
**Example 1**
```text
Input: nums = [4, 1, 4, 2, 1, 4, 3], k = 2
Output: [4, 1]
```
`4` occurs three times, `1` twice, and `2` and `3` once each.
**Example 2**
```text
Input: nums = [3, 3, 2, 2, 1, 1, 0], k = 2
Output: [1, 2]
```
`1`, `2` and `3` each occur twice. The tie is broken by the smaller value, so `1` and `2` are returned and `3` is not.
Constraints
- 1 <= len(nums) <= 10^5
- -10^9 <= nums[i] <= 10^9
- 1 <= k <= d, where d is the number of distinct values in nums
Examples
Input: ([4, 1, 4, 2, 1, 4, 3], 2)
Expected Output: [4, 1]
Explanation: Source example 1: 4 occurs three times, 1 twice, 2 and 3 once each.
Input: ([3, 3, 2, 2, 1, 1, 0], 2)
Expected Output: [1, 2]
Explanation: Source example 2: 1, 2 and 3 tie at two occurrences; the two smallest are kept and 3 is excluded.
Hints
- Start by finding out how many times each distinct value occurs in nums.
- The ranking is one total order: a higher count always comes first, and between equal counts the smaller value comes first. The answer is the first k values of that order.
- Check what happens when several values share the frequency at the k-th position: only the smaller ones make the cut.