Dot Product of Run-Length Encoded Vectors with Long Repeated Runs
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
The vectors in this problem contain long stretches of consecutive repeated values, for example `[4, 4, 4, 4, 5, 5, 5, 5]`, so storing every element wastes memory and time. Each vector is therefore stored as a list of runs: a run `[value, length]` stands for `value` repeated `length` times in a row, and the vector is the concatenation of its runs in order.
Given two vectors of the same total length in this form, return their dot product: the sum over every position `i` of `a[i] * b[i]`, where `a` and `b` are the expanded vectors.
### Function Signature
```python
def rle_dot_product(a_runs: list[list[int]], b_runs: list[list[int]]) -> int:
```
### Rules
- Each run is a pair `[value, length]` with `length >= 1`.
- Adjacent runs in the same vector may have equal values (runs are not necessarily maximal); the vector is still the concatenation of all its runs.
- The run boundaries of the two vectors need not line up.
- Both vectors have the same total length, the sum of their run lengths.
- Return the exact integer dot product.
### Constraints
- `1 <= len(a_runs) <= 10^5` and `1 <= len(b_runs) <= 10^5`
- `1 <= length <= 10^9` for every run
- The two vectors have the same total length, which is at most `10^9`
- `-1000 <= value <= 1000` for every run
- The absolute value of the result is at most `10^15`. It can exceed `2^31 - 1`, so use 64-bit (or arbitrary-precision) integer arithmetic; it always lies within `-2^53` to `2^53`.
### Examples
**Example 1**
```text
Input: a_runs = [[4, 4], [5, 4]], b_runs = [[1, 2], [2, 3], [3, 3]]
Output: 79
```
The expanded vectors are `[4, 4, 4, 4, 5, 5, 5, 5]` and `[1, 1, 2, 2, 2, 3, 3, 3]`, so the dot product is `4*1 + 4*1 + 4*2 + 4*2 + 5*2 + 5*3 + 5*3 + 5*3 = 79`.
**Example 2**
```text
Input: a_runs = [[2, 3], [2, 2], [-1, 5]], b_runs = [[0, 4], [3, 6]]
Output: -9
```
The expanded vectors are `[2, 2, 2, 2, 2, -1, -1, -1, -1, -1]` and `[0, 0, 0, 0, 3, 3, 3, 3, 3, 3]`. The first four positions contribute 0, position 4 contributes `2*3 = 6`, and the last five positions contribute `-1*3` each, for a total of `6 - 15 = -9`.
**Example 3**
```text
Input: a_runs = [[1000, 1000000000]], b_runs = [[-1000, 1000000000]]
Output: -1000000000000000
```
Each of the `10^9` positions contributes `1000 * -1000 = -10^6`, so the result is `-10^15`.
Overview: A coding problem on vectors with long runs of repeated values: given two run-length-encoded vectors of equal total length, return their exact dot product. It tests merging runs whose boundaries do not line up, handling vector lengths up to a billion, and integer results beyond 32 bits.
Some vectors contain long stretches of consecutive repeated values, for example `[4, 4, 4, 4, 5, 5, 5, 5]`, so storing every element wastes memory and time. Each vector is therefore stored as a list of runs: a run `[value, length]` stands for `value` repeated `length` times in a row, and the vector is the concatenation of its runs in order.
Given two vectors of the same total length in this form, `a_runs` and `b_runs`, return their dot product: the sum over every position `i` of `a[i] * b[i]`, where `a` and `b` are the expanded vectors.
Implement `rle_dot_product(a_runs, b_runs)`. Each argument is a list of `[value, length]` pairs (Java: `int[][]`, C++: `std::vector<std::vector<int>>`); return the dot product as an integer (Java: `long`, C++: `long long`).
### Rules
- Each run is a pair `[value, length]` with `length >= 1`.
- Adjacent runs in the same vector may have equal values (runs are not necessarily maximal); the vector is still the concatenation of all its runs.
- The run boundaries of the two vectors need not line up.
- Both vectors have the same total length, the sum of their run lengths.
- Return the exact integer dot product.
### Constraints
- `1 <= len(a_runs) <= 10^5` and `1 <= len(b_runs) <= 10^5`
- `1 <= length <= 10^9` for every run
- The two vectors have the same total length, which is at most `10^9`
- `-1000 <= value <= 1000` for every run
- The absolute value of the result is at most `10^15`. It can exceed `2^31 - 1`, so use 64-bit (or arbitrary-precision) integer arithmetic (Java `long`, C++ `long long`); it always lies within `-2^53` to `2^53`.
### Example 1
```text
Input: a_runs = [[4, 4], [5, 4]], b_runs = [[1, 2], [2, 3], [3, 3]]
Output: 79
```
The expanded vectors are `[4, 4, 4, 4, 5, 5, 5, 5]` and `[1, 1, 2, 2, 2, 3, 3, 3]`, so the dot product is `4*1 + 4*1 + 4*2 + 4*2 + 5*2 + 5*3 + 5*3 + 5*3 = 79`.
### Example 2
```text
Input: a_runs = [[2, 3], [2, 2], [-1, 5]], b_runs = [[0, 4], [3, 6]]
Output: -9
```
The expanded vectors are `[2, 2, 2, 2, 2, -1, -1, -1, -1, -1]` and `[0, 0, 0, 0, 3, 3, 3, 3, 3, 3]`. The first four positions contribute 0, position 4 contributes `2*3 = 6`, and the last five positions contribute `-1*3` each, for a total of `6 - 15 = -9`.
Constraints
- 1 <= len(a_runs) <= 10^5 and 1 <= len(b_runs) <= 10^5
- Each run is a pair [value, length] with 1 <= length <= 10^9
- The two vectors have the same total length (the sum of their run lengths), which is at most 10^9
- -1000 <= value <= 1000 for every run
- Adjacent runs in the same vector may have equal values, and the run boundaries of the two vectors need not line up
- |result| <= 10^15: it can exceed 2^31 - 1 (use Java long / C++ long long) and always lies within -2^53 to 2^53
Examples
Input: ([[4, 4], [5, 4]], [[1, 2], [2, 3], [3, 3]])
Expected Output: 79
Explanation: Source Example 1: misaligned boundaries; the overlaps contribute 4*1*2 + 4*2*2 + 5*2*1 + 5*3*3 = 8 + 16 + 10 + 45 = 79.
Input: ([[2, 3], [2, 2], [-1, 5]], [[0, 4], [3, 6]])
Expected Output: -9
Explanation: Source Example 2: adjacent equal-valued runs in a, zeros in b and a negative value; 0 + 0 + 6 - 15 = -9.
Hints
- The expanded vectors can have up to 10^9 positions while each vector has at most 10^5 runs, so aim for work proportional to the number of runs rather than the length.
- Over any stretch of positions where neither vector changes value, every position contributes the same product.
- The run boundaries of the two vectors need not coincide; think about at which positions the pair (a[i], b[i]) can change.