FastPrepExpand a Run-Length Iterator

Expand a Run-Length Iterator

Google logoGoogle● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

A run-length encoding is an array of pairs [value,count]. The iterator would return value exactly count times before advancing to the next pair.

Return the complete finite iterator output. Pairs with count zero contribute nothing. Preserve pair order and repeated values in different pairs.

Function

expandRuns(runs: int[][]) → int[]

Examples

Example 1

runs = [[1,2],[4,3]]return = [1,1,4,4,4]

Value 1 appears twice and value 4 appears three times.

Example 2

runs = [[5,0],[2,1]]return = [2]

The zero-count run is skipped.

Example 3

runs = []return = []

An empty iterator has no output.

Constraints

  • 0 <= runs.length <= 100000.
  • Each row is [value,count] with signed integer value and 0 <= count.
  • The total output length is at most 200000.

More Google problems

See Google hiring insights
public int[] expandRuns(int[][] runs) {
    // Write your code here.
}
runs[[1,2],[4,3]]
expected[1,1,4,4,4]
Checking account…