Quick 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.

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

  1. 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.
  2. Over any stretch of positions where neither vector changes value, every position contributes the same product.
  3. The run boundaries of the two vectors need not coincide; think about at which positions the pair (a[i], b[i]) can change.

Dot Product of Two Run-Length Encoded Vectors

mediumMetaMachine Learning Engineer

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

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

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.

Examples

Example 1
Input
([[4, 4], [5, 4]], [[1, 2], [2, 3], [3, 3]])
Output
79
Notes
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.
Example 2
Input
([[2, 3], [2, 2], [-1, 5]], [[0, 4], [3, 6]])
Output
-9
Notes
Source Example 2: adjacent equal-valued runs in a, zeros in b and a negative value; 0 + 0 + 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

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def rle_dot_product(a_runs: list[list[int]], b_runs: list[list[int]]) -> int:
Sample call
rle_dot_product([[4, 4], [5, 4]], [[1, 2], [2, 3], [3, 3]])
Language
Case 1
Input
([[4, 4], [5, 4]], [[1, 2], [2, 3], [3, 3]])
Expected
79
Case 2
Input
([[2, 3], [2, 2], [-1, 5]], [[0, 4], [3, 6]])
Expected
-9

Browse More Questions

Show the approach

Approach

Walk both run lists at once with one index per list and the number of positions still unconsumed in each current run (rem_a, rem_b). At every step the next take = min(rem_a, rem_b) positions lie inside a single run of a and a single run of b, so every one of them contributes the same product value_a * value_b; add value_a * value_b * take to the total and subtract take from both remainders. A run whose remainder reaches zero is finished, so its index advances and the next run's length is loaded; when both runs end at the same position, both indices advance in the same step. Invariant: before each step both pointers stand at the same position of the expanded vectors (they always consume equal amounts), and the total equals the exact dot product of the prefix consumed so far. Every step finishes at least one run, so there are at most len(a_runs) + len(b_runs) steps, and because the total lengths are equal both lists run out together, at which point the prefix is the whole vector. Edge cases: adjacent runs with equal values need no merging because each run is processed on its own; zero values simply contribute zero; a single step contributes at most 10^6 * 10^9 = 10^15 in absolute value and every partial sum is bounded by the sum of |a[i] * b[i]| <= 10^15, so 64-bit integers never overflow and JavaScript numbers stay exact below 2^53. Java and C++ widen each value to long / long long before multiplying, because value * value * length overflows 32 bits.

Time complexity:
O(n + m), where n = len(a_runs) and m = len(b_runs)
Space complexity:
O(1)