FastPrepMerge K Sorted Arrays Without Duplicates

Merge K Sorted Arrays Without Duplicates

Amazon logoAmazon● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Given k integer arrays in arrays, where every array is sorted in nondecreasing order, merge them into one sorted array.

Return every distinct value exactly once, even when a value is repeated within one input array or across several arrays.

Function

mergeSortedUnique(arrays: int[][]) → int[]

Examples

Example 1

arrays = [[1,2,2,7],[2,3,7],[1,5,8]]return = [1,2,3,5,7,8]

The merge preserves sorted order while emitting shared values such as 1, 2, and 7 only once.

Example 2

arrays = [[],[-3,-3,0],[],[0,4]]return = [-3,0,4]

Empty arrays contribute nothing, and duplicates are removed from the merged result.

Constraints

  • 1 <= arrays.length <= 10^5.
  • Every input array is sorted in nondecreasing order.
  • The total number of values across all arrays is at most 2 * 10^5.
  • Every value is a signed 32-bit integer.

More Amazon problems

See Amazon hiring insights
public int[] mergeSortedUnique(int[][] arrays) {
    // write your code here
}
arrays[[1,2,2,7],[2,3,7],[1,5,8]]
expected[1,2,3,5,7,8]
Checking account…