Count Occurrences of Each Value in an Integer List, Sorted by Value
Company: Apple
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Given a list of integers that may contain repeated values, count how many times each distinct value appears.
The report describes the task only as counting the occurrences of each number in a list with duplicates; this version fixes the output format so that the answer is unique.
### Function Signature
```python
def count_occurrences(nums: list[int]) -> list[list[int]]:
```
### Rules
- Return one pair `[value, count]` for every distinct value in `nums`, where `count` is the number of times `value` appears in `nums`.
- Sort the pairs by `value` in ascending numeric order.
- Each distinct value appears in exactly one pair, so the counts add up to `len(nums)`.
### Constraints
- `1 <= len(nums) <= 10^5`
- `-10^9 <= nums[i] <= 10^9`; values may be negative, zero or repeated.
- Every value and every count fits in a 32-bit signed integer.
### Examples
**Example 1**
```text
Input: nums = [3, 1, 3, 2, 1, 3]
Output: [[1, 2], [2, 1], [3, 3]]
```
The value 1 appears twice, 2 once and 3 three times.
**Example 2**
```text
Input: nums = [-5, 0, -5, 7]
Output: [[-5, 2], [0, 1], [7, 1]]
```
Negative values sort before zero.
**Example 3**
```text
Input: nums = [4, 4, 4, 4]
Output: [[4, 4]]
```
A single distinct value produces a single pair.
Overview: A short coding question: given a list of integers with repeated values, return each distinct value together with the number of times it appears, sorted by value. It tests frequency counting, handling negative numbers, and producing a deterministic output order.
Read the full Apple Data Scientist interview experience this question came from
Given a list of integers `nums` that may contain repeated values, count how many times each distinct value appears.
Return a list of pairs `[value, count]`, one pair for every distinct value in `nums`, where `count` is the number of times `value` appears in `nums`. Sort the pairs by `value` in ascending numeric order. Each distinct value appears in exactly one pair, so the counts add up to `len(nums)`.
No value or count can exceed 2^31 - 1: every value and every count fits in a 32-bit signed integer.
### Constraints
- `1 <= len(nums) <= 10^5`
- `-10^9 <= nums[i] <= 10^9`; values may be negative, zero or repeated.
- Every value and every count fits in a 32-bit signed integer.
### Example 1
```text
Input: nums = [3, 1, 3, 2, 1, 3]
Output: [[1, 2], [2, 1], [3, 3]]
```
The value 1 appears twice, 2 once and 3 three times.
### Example 2
```text
Input: nums = [-5, 0, -5, 7]
Output: [[-5, 2], [0, 1], [7, 1]]
```
Negative values sort before zero.
Constraints
- 1 <= len(nums) <= 10^5
- -10^9 <= nums[i] <= 10^9; values may be negative, zero or repeated.
- Every value and every count fits in a 32-bit signed integer (none exceeds 2^31 - 1).
Examples
Input: ([5],)
Expected Output: [[5, 1]]
Explanation: Minimum valid: a single element gives one pair with count 1.
Input: ([-1000000000],)
Expected Output: [[-1000000000, 1]]
Explanation: Singleton at the lower value bound -10^9.
Hints
- Every distinct value gets exactly one pair, so the counts in your answer must add up to len(nums).
- Equal values are not necessarily next to each other in the input; make sure all occurrences of a value are counted together.
- The output order is fixed by the numeric value in each pair, not by input order or by count; negative values come before zero.