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

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

  1. 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.
  2. Over any stretch of positions where neither vector changes value, every position adds the same product to the answer.
  3. The answer can exceed the 32-bit integer range, and so can a running total partway through.

Dot Product of Two Run-Length Encoded Vectors

hardMetaApplied Scientist

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

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

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.

Examples

Example 1
Input
([[5, 1]], [[-3, 1]])
Output
-15
Notes
Minimum valid input: one run of count 1 in each vector, decoded length 1, so the answer is 5 * (-3) = -15.
Example 2
Input
([[1, 3], [2, 2]], [[3, 1], [4, 4]])
Output
27
Notes
Source example 1: [1, 1, 1, 2, 2] . [3, 4, 4, 4, 4] = 3 + 4 + 4 + 8 + 8 = 27.

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

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: list[list[int]], b: list[list[int]]) -> int:
Sample call
rle_dot_product([[5, 1]], [[-3, 1]])
Language
Case 1
Input
([[5, 1]], [[-3, 1]])
Expected
-15
Case 2
Input
([[1, 3], [2, 2]], [[3, 1], [4, 4]])
Expected
27

Browse More Questions

Show the approach

Approach

Never decode the vectors; walk both encodings at once. Keep one index per encoding (i into a, j into b) and the number of not-yet-consumed positions left in each current run (rem_a, rem_b). At every step the next take = min(rem_a, rem_b) positions lie where both vectors hold a constant value, so they contribute a[i][0] * b[j][0] * take. Subtract take from both remainders, and advance every index whose run is now exhausted: one of them, or both when the two runs end at the same position.

Invariant: every position before the current one has been added to the total exactly once, and rem_a / rem_b are exactly the unconsumed parts of the current runs. Because take >= 1 and at least one run finishes on each step, the loop runs at most len(a) + len(b) times, independent of the decoded length (up to 10^9). When the loop ends, both encodings are exhausted together, because their decoded lengths are equal, so every position has been counted.

Edge cases: runs that end together advance both indices in the same step; adjacent runs with equal values need no merging; zero values add nothing; the running total and a single block product (up to 1000 * 1000 * 10^9 = 10^15) need 64-bit integers, which is why Java uses long and C++ uses long long. Every partial sum stays within 10^15 in absolute value because the decoded length is at most 10^9, so the JavaScript number stays exact. An empty encoding, which the constraints exclude, would return 0.

Time complexity:
O(len(a) + len(b))
Space complexity:
O(1)