FastPrepRun-Length Encoded Vector Dot Product
Problems/Run-Length Encoded Vector Dot Product

Run-Length Encoded Vector Dot Product

Meta logoMeta● MediumFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Meta machine learning engineer onsite interview, in an ML coding round. The task was to find a fast way to take the dot product of two vectors that have long runs of the same number. The report gives one example.

The report shows the vectors as plain lists and asks you to pick a good way to store them. It gives no limits and no function. Here, each vector already comes as runs of value and count, and a vector can be far too long to write out. The report's example is Example 1.

Our version is about 85% the same as the reported question.

Given two vectors of the same length in run-length form, encoded1 and encoded2, return their dot product.

In run-length form, a vector is a list of rows [value, count], read from left to right. Each row stands for count copies of value in a row. For example, the vector [4,4,4,4,5,5,5,5] is stored as [[4,4],[5,4]]. Two neighboring rows may have the same value.

The dot product of two vectors a and b of length n is a[0] * b[0] + a[1] * b[1] + ... + a[n - 1] * b[n - 1].

A vector can have up to 10^10 entries, far too many to write out, so work with the runs directly.

Function

dotProduct(encoded1: int[][], encoded2: int[][]) → long

Examples

Example 1

encoded1 = [[4,4],[5,4]]encoded2 = [[1,2],[2,3],[3,3]]return = 79

The vectors are [4,4,4,4,5,5,5,5] and [1,1,2,2,2,3,3,3]. The dot product is 4*1 + 4*1 + 4*2 + 4*2 + 5*2 + 5*3 + 5*3 + 5*3 = 79.

Example 2

encoded1 = [[2,3],[2,2],[-1,5]]encoded2 = [[0,4],[3,6]]return = -9

The 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 add 0, position 4 adds 2*3 = 6, and the last five add -1*3 = -3 each, so the answer is 6 - 15 = -9.

Example 3

encoded1 = [[10000,1000000000],[7,3]]encoded2 = [[10000,999999999],[1,4]]return = 99999999900010021

Both vectors have 1000000003 entries. The first 999999999 positions each add 10000*10000 = 10^8. The next position adds 10000*1, and the last three add 7*1 each. The total needs a 64-bit integer.

Constraints

  • 1 <= encoded1.length, encoded2.length <= 10^4
  • encoded1[i].length == encoded2[j].length == 2
  • Each row is [value, count] with -10^4 <= value <= 10^4 and 1 <= count <= 10^9.
  • The counts in encoded1 and the counts in encoded2 add up to the same length n, and n <= 10^10.

More Meta Onsite Interview questions

  • Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
  • Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
  • Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
  • Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
  • Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
  • Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
  • Maximum Stock Profit with Transaction LimitsONSITE INTERVIEW · Seen Aug 2026

Similar questions at other companies

  • Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • Interval List IntersectionsGrammarly · PHONE SCREEN
  • Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
  • Subarray Product Less Than KApple · PHONE SCREENApple interview questions
  • Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
  • 3SumAmazon · PHONE SCREEN
  • 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Subarray Product Less Than KAngel One · PHONE SCREEN
  • Subarray Product Less Than KThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
  • Trapping Rain WaterGoldman Sachs · PHONE SCREENGoldman Sachs interview questions

Meta interview experiences

  • Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
  • Meta Business Support Engineer Phone Screen: 1 Experience
  • Meta Software Engineer Onsite Questions: 3 Experiences
  • Meta AI Native Engineer OA and Onsite: 1 Experience
  • Meta AI-Native Coding Interview: 3 Experiences
  • Meta Production Engineer Intern Questions: 6 Experiences
All Meta interview questionsSee Meta hiring insights
Saved
class Solution {
    public long dotProduct(int[][] encoded1, int[][] encoded2) {
        // Write your code here.
    }
}
Checking account…