FastPrepMerge K Sorted Sequences

Merge K Sorted Sequences

Headlands Technologies logoHeadlands Technologies● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given sequences, an array of integer sequences, merge all their entries into one nondecreasing array. Every individual sequence is already sorted in nondecreasing order.

Include every input entry exactly once, preserving duplicates. Empty sequences are allowed. If there are no entries, return an empty array. Equal keys are indistinguishable in the returned integer array, so no additional record tie rule is needed. Do not modify any input sequence.

This finite function-runner exercise represents the sort keys of the original task's unspecified stock records. It introduces no trading orders, prices, client request/read protocol or concurrent workers.

Function

mergeSortedSequences(sequences: int[][]) → int[]

Examples

Example 1

sequences = [[1,4,7],[2,5,8],[3,6,9]]return = [1,2,3,4,5,6,7,8,9]

Each row is sorted, but the rows interleave. Taking the smallest available head gives the values1 through9.

Example 2

sequences = [[],[-5,-5,4],[-5,0],[]]return = [-5,-5,-5,0,4]

Empty rows contribute nothing. Preserve all three occurrences of-5, followed by0 and4.

Example 3

sequences = []return = []

There are no sequences, so the result is an empty array.

Constraints

  • 0 <= sequences.length <= 100.
  • The total number of entries across all rows is at most 2000.
  • Each row is sorted; every value is an integer in [-10^9,10^9].
  • Rows contain no null values. All output values fit signed 32-bit integers.
  • These numerical limits and the integer-array adapter are practice conventions; the report did not supply them.

More Headlands Technologies problems

See Headlands Technologies hiring insights
public int[] mergeSortedSequences(int[][] sequences) {
    // write your code here
}
sequences[[1,4,7],[2,5,8],[3,6,9]]
expected[1,2,3,4,5,6,7,8,9]
Checking account…