Dot Product of Two Run-Length Encoded Vectors
Company: Meta
Role: Applied Scientist
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
A vector that contains long stretches of equal consecutive values can be stored compactly with run-length encoding: a list of runs `[value, count]`, each meaning `value` repeated `count` times. For example, the vector `[4, 4, 4, 0, 0, 7]` is stored as `[[4, 3], [0, 2], [7, 1]]`, using memory proportional to the number of runs rather than to the length of the vector.
Given two vectors of the same length, both stored this way, return their dot product: the sum, over every position, of the product of the two vectors' values at that position. The decoded vectors can be far longer than their encodings.
### Function Signature
```python
def rle_dot_product(a: list[list[int]], b: list[list[int]]) -> int:
```
### Rules
- Each run is a pair `[value, count]` with `count >= 1`; the runs, in order, describe the vector from its first position to its last.
- Adjacent runs may hold the same value; such an encoding is still valid and decodes in the same way.
- The two encodings may split the vector into runs at different positions and may contain different numbers of runs, but the decoded vectors always have the same length.
- Return the exact integer dot product of the two decoded vectors.
### Constraints
- `1 <= len(a) <= 10^5` and `1 <= len(b) <= 10^5`
- `1 <= count <= 10^9` for every run
- `-1000 <= value <= 1000` for every run
- The sum of the counts in `a` equals the sum of the counts in `b`, and this decoded length is at most `10^9`.
- The answer can exceed `2^31 - 1` in absolute value (its absolute value is at most `10^15`), so languages with fixed-width integers need a 64-bit result.
### Examples
**Example 1**
```text
Input: a = [[1, 3], [2, 2]], b = [[3, 1], [4, 4]]
Output: 27
```
`a` decodes to `[1, 1, 1, 2, 2]` and `b` to `[3, 4, 4, 4, 4]`, so the dot product is `1*3 + 1*4 + 1*4 + 2*4 + 2*4 = 27`.
**Example 2**
```text
Input: a = [[5, 1], [-2, 1], [5, 1]], b = [[1, 2], [3, 1]]
Output: 18
```
`a` decodes to `[5, -2, 5]` and `b` to `[1, 1, 3]`, so the dot product is `5 - 2 + 15 = 18`.
**Example 3**
```text
Input: a = [[2, 1000000000]], b = [[-1000, 400000000], [1000, 600000000]]
Output: 400000000000
```
The first 400,000,000 positions contribute `2 * (-1000)` each and the remaining 600,000,000 positions contribute `2 * 1000` each: `-800000000000 + 1200000000000 = 400000000000`.
Overview: Two long integer vectors are stored as run-length encoded lists of value and count pairs, and you must return their exact dot product without expanding them. Tests working directly on compressed runs, handling encodings whose runs split at different positions, and awareness of 64-bit overflow.
Read the full Meta Applied Scientist interview experience this question came from
A vector with long stretches of equal consecutive values can be stored with run-length encoding: a list of runs `[value, count]`, each meaning `value` repeated `count` times. The runs, in order, describe the vector from its first position to its last. For example, the vector `[4, 4, 4, 0, 0, 7]` is stored as `[[4, 3], [0, 2], [7, 1]]`, using memory proportional to the number of runs rather than to the length of the vector.
You are given two vectors of the same length, `a` and `b`, both stored this way. Return their dot product: the sum, over every position, of the product of the two vectors' values at that position. The decoded vectors can be far longer than their encodings.
Rules:
- Each run is a pair `[value, count]` with `count >= 1`.
- Adjacent runs may hold the same value; such an encoding is still valid and decodes in the same way.
- The two encodings may split the vector into runs at different positions and may contain different numbers of runs, but the decoded vectors always have the same length.
- Return the exact integer dot product of the two decoded vectors.
**Example 1**
```text
Input: a = [[1, 3], [2, 2]], b = [[3, 1], [4, 4]]
Output: 27
```
`a` decodes to `[1, 1, 1, 2, 2]` and `b` to `[3, 4, 4, 4, 4]`, so the dot product is `1*3 + 1*4 + 1*4 + 2*4 + 2*4 = 27`.
**Example 2**
```text
Input: a = [[2, 1000000000]], b = [[-1000, 400000000], [1000, 600000000]]
Output: 400000000000
```
The first 400,000,000 positions contribute `2 * (-1000)` each and the remaining 600,000,000 positions contribute `2 * 1000` each: `-800000000000 + 1200000000000 = 400000000000`.
**Constraints**
- `1 <= len(a) <= 10^5` and `1 <= len(b) <= 10^5`
- `1 <= count <= 10^9` for every run
- `-1000 <= value <= 1000` for every run
- The sum of the counts in `a` equals the sum of the counts in `b`, and this decoded length is at most `10^9`.
- The answer can exceed `2^31 - 1` in absolute value (its absolute value is at most `10^15`), so Java returns `long` and C++ returns `long long`.
Constraints
- 1 <= len(a) <= 10^5 and 1 <= len(b) <= 10^5
- 1 <= count <= 10^9 for every run
- -1000 <= value <= 1000 for every run
- The sum of the counts in a equals the sum of the counts in b, and this decoded length is at most 10^9.
- The answer can exceed 2^31 - 1 in absolute value (its absolute value is at most 10^15), so a 64-bit result is required (Java long, C++ long long).
Examples
Input: ([[5, 1]], [[-3, 1]])
Expected Output: -15
Explanation: Minimum valid input: one run of count 1 in each vector, decoded length 1, so the answer is 5 * (-3) = -15.
Input: ([[1, 3], [2, 2]], [[3, 1], [4, 4]])
Expected Output: 27
Explanation: Source example 1: [1, 1, 1, 2, 2] . [3, 4, 4, 4, 4] = 3 + 4 + 4 + 8 + 8 = 27.
Hints
- The decoded vectors can have up to 10^9 positions, far more than their encodings, so expanding them into full arrays is too slow and uses too much memory.
- Over any stretch of positions where neither vector changes value, every position adds the same product to the answer.
- The answer can exceed the 32-bit integer range, and so can a running total partway through.