FastPrepMerge Sorted Arrays
Problems/Merge Sorted Arrays

Merge Sorted Arrays

Indeed logoIndeed● MediumINTERNPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: A phone screen report names the task as merging sorted arrays, with a follow-up that merges k sorted arrays.

The report gives no input format, output or limits. Here, you get all the sorted arrays at once and return one new sorted array with every value, repeats kept. Two arrays is the base question, and the follow-up is the general case, so both are judged together. Empty arrays and the size limits are practice choices.

The judged task matches the report at about 80%.

You are given arrays, a list of integer arrays. Each array is sorted in non-decreasing order, and some arrays may be empty.

Merge them into one array sorted in non-decreasing order and return it. Keep every value, including repeats: a value that appears three times in total appears three times in the answer.

With two arrays this is the classic merge of two sorted arrays. The general case merges any number of them.

Function

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

Examples

Example 1

arrays = [[1,4,4,9],[2,4,10]]return = [1,2,4,4,4,9,10]

Two arrays, the base case. Taking the smaller front value each time gives 1, 2, then the three 4s (two from the first array and one from the second), then 9 and 10. Duplicates are kept.

Example 2

arrays = [[-3,0],[],[-5,-3,8]]return = [-5,-3,-3,0,8]

The middle array is empty and adds nothing. The other two merge into -5, -3, -3, 0, 8.

Example 3

arrays = [[7]]return = [7]

With one array, the answer is that array.

Constraints

  • 1 <= arrays.length <= 500
  • 0 <= arrays[i].length <= 2000
  • The total number of values in all arrays is at most 2000.
  • -10^9 <= arrays[i][j] <= 10^9
  • Each arrays[i] is sorted in non-decreasing order.

More Indeed problems

  • Insert into an Unrolled Linked ListPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Course Completion OrderPHONE SCREEN · Seen May 2025
  • Recipes You Can PreparePHONE SCREEN · Seen May 2025
All Indeed interview questionsSee Indeed hiring insights
Saved
class Solution {
    public int[] mergeSortedArrays(int[][] arrays) {
        // Write your code here.
    }
}
Checking account…