PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates competency in implementing and generalizing merge operations on sorted arrays, exercising algorithmic problem-solving, array manipulation, handling of edge cases (empty arrays, negative numbers, duplicates), and analysis of time and space complexity.

  • medium
  • Fora Travel
  • Coding & Algorithms
  • Software Engineer

Merge Sorted Arrays

Company: Fora Travel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

You are given two related tasks about sorted integer arrays. 1. Implement a function that takes two arrays sorted in nondecreasing order and returns a new sorted array containing all elements from both inputs. 2. Generalize the solution to merge K arrays, where each array is sorted in nondecreasing order, and return one sorted array containing all elements. Requirements: - Preserve duplicates. - Handle empty arrays, negative numbers, and arrays of different lengths. - Return a new array rather than mutating the inputs. - Discuss the time and space complexity of your approach.

Quick Answer: This question evaluates competency in implementing and generalizing merge operations on sorted arrays, exercising algorithmic problem-solving, array manipulation, handling of edge cases (empty arrays, negative numbers, duplicates), and analysis of time and space complexity.

Part 1: Merge Two Sorted Arrays

Given two integer arrays a and b sorted in nondecreasing order, return a new array containing all elements from both arrays in nondecreasing order. Preserve duplicates, handle empty arrays and negative numbers, and do not mutate the inputs.

Constraints

  • 0 <= len(a), len(b) <= 100000
  • -1000000000 <= a[i], b[i] <= 1000000000
  • Both input arrays are already sorted in nondecreasing order
  • The solution must return a new array rather than modifying the inputs

Examples

Input: ([1, 3, 5], [2, 4, 6])

Expected Output: [1, 2, 3, 4, 5, 6]

Explanation: A standard merge of two equal-length sorted arrays.

Input: ([-3, -1, 2], [-2, 2, 2, 7])

Expected Output: [-3, -2, -1, 2, 2, 2, 7]

Explanation: Handles negative values, duplicates, and different lengths.

Input: ([], [1, 2, 3])

Expected Output: [1, 2, 3]

Explanation: If one array is empty, the result is the other array.

Input: ([], [])

Expected Output: []

Explanation: Edge case where both arrays are empty.

Input: ([5], [5])

Expected Output: [5, 5]

Explanation: Single-element arrays with equal values must preserve duplicates.

Hints

  1. Keep one pointer in each array and compare the current elements.
  2. After one array is exhausted, append the remaining elements from the other array.

Part 2: Merge K Sorted Arrays

Given a list of K integer arrays, where each array is sorted in nondecreasing order, return one new array containing all elements from all arrays in nondecreasing order. Preserve duplicates, handle empty arrays and negative numbers, and do not mutate the inputs.

Constraints

  • 0 <= K <= 10000, where K is the number of arrays
  • 0 <= total number of elements across all arrays <= 200000
  • -1000000000 <= arrays[i][j] <= 1000000000
  • Each inner array is already sorted in nondecreasing order
  • The solution must return a new array rather than modifying the inputs

Examples

Input: ([[1, 4, 5], [1, 3, 4], [2, 6]],)

Expected Output: [1, 1, 2, 3, 4, 4, 5, 6]

Explanation: A classic example with three sorted arrays.

Input: ([[], [-2, -1, 3], [0, 0, 2]],)

Expected Output: [-2, -1, 0, 0, 2, 3]

Explanation: Handles empty inner arrays, negative numbers, and duplicates.

Input: ([[1, 2, 3]],)

Expected Output: [1, 2, 3]

Explanation: If there is only one array, the result is that array copied into a new list.

Input: ([],)

Expected Output: []

Explanation: Edge case where there are no arrays at all.

Input: ([[], [], []],)

Expected Output: []

Explanation: Edge case where every array is empty.

Hints

  1. At any moment, you only need to know the smallest current unused element from each array.
  2. A min-heap can help you repeatedly extract the next smallest element in better than O(K) per step.
Last updated: May 23, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.