Magicsheet logo

Product of Two Run-Length Encoded Arrays

Medium
50%
Updated 8/1/2025

Asked by 2 Companies

Product of Two Run-Length Encoded Arrays

What is this problem about?

The Product of Two Run-Length Encoded Arrays problem gives you two arrays in run-length encoding format (value, count pairs) and asks you to compute their element-wise product, returned as a run-length encoded array. This coding problem uses a two-pointer merge on the encoded formats. The array and two pointers interview pattern is demonstrated.

Why is this asked in interviews?

Yandex and Meta ask this because it tests the ability to process run-length encoded data without decoding — a technique used in image compression, genomics, and data streaming. The two-pointer approach merges runs while tracking remaining counts.

Algorithmic pattern used

Two-pointer on encoded arrays. Maintain pointers i, j for encoded1 and encoded2. At each step: compute product of current values. Take count = min(encoded1[i][1], encoded2[j][1]). Append (product, count) to result (merging with previous if same product). Reduce counts: encoded1[i][1] -= count; if 0, advance i. Same for j.

Example explanation

encoded1=[(1,3),(2,3)], encoded2=[(6,3),(3,3)].

  • Both start with count 3. Product = 1*6=6. count=3. Result: [(6,3)].
  • encoded1 moves to (2,3). Product=2*3=6. count=3. Merge with previous (same product 6): [(6,6)]. Result: [(6,6)].

Common mistakes candidates make

  • Decoding both arrays (O(total elements) instead of O(unique runs)).
  • Not merging consecutive equal products in the result.
  • Off-by-one in count management.
  • Not handling unequal run lengths (must take min and advance the depleted one).

Interview preparation tip

Run-length encoded operations require processing chunks without full decoding. The two-pointer merging approach is O(n+m) where n,m are numbers of runs — much better than O(decoded length). Practice similar encoded array operations: "add/subtract two RLE arrays," "multiply two RLE arrays." The key insight: take the minimum chunk size from both arrays at each step, advancing whichever run is exhausted.

Similar Questions