PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Microsoft

Merge multiple sorted arrays using min-heap

Last updated: Mar 29, 2026

Quick Overview

This question evaluates algorithm design and data structure proficiency, focusing on merging multiple sorted sequences and reasoning about time and space complexity for large numbers of arrays and elements.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Merge multiple sorted arrays using min-heap

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

### Problem You are given `k` sorted arrays of integers: - `arrays[0], arrays[1], ..., arrays[k-1]` - Each `arrays[i]` is sorted in **non-decreasing** order. - The total number of elements across all arrays is `N`. Your task is to merge these `k` sorted arrays into a **single sorted array** (or list) containing all `N` elements. You should design an algorithm that is **asymptotically optimal** in terms of time complexity for large `k` and `N`. A straightforward approach that repeatedly scans all array heads at each step is too slow when `k` is large. **Output**: a single sorted sequence of all elements from the `k` arrays. #### Example Input: - `k = 3` - `arrays[0] = [1, 4, 9]` - `arrays[1] = [2, 6]` - `arrays[2] = [0, 3, 7, 8]` Output: - `[0, 1, 2, 3, 4, 6, 7, 8, 9]` #### Constraints - `1 <= k <= 10^5` - Each array length can be zero or more. - Total number of elements `N` across all arrays: `0 <= N <= 10^6` (assume this upper bound for complexity discussion). - Integer values fit in 32-bit signed range. #### Requirements 1. Describe an efficient algorithm for this problem, including: - Data structures you would use. - Time and space complexity. 2. Then implement the function that merges the arrays following your algorithm. *(Hint: Consider a priority-based data structure that efficiently gives you the smallest current element among the `k` arrays.)*

Quick Answer: This question evaluates algorithm design and data structure proficiency, focusing on merging multiple sorted sequences and reasoning about time and space complexity for large numbers of arrays and elements.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
Microsoft logo
Microsoft
Oct 28, 2025, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
2
0

Problem

You are given k sorted arrays of integers:

  • arrays[0], arrays[1], ..., arrays[k-1]
  • Each arrays[i] is sorted in non-decreasing order.
  • The total number of elements across all arrays is N .

Your task is to merge these k sorted arrays into a single sorted array (or list) containing all N elements.

You should design an algorithm that is asymptotically optimal in terms of time complexity for large k and N. A straightforward approach that repeatedly scans all array heads at each step is too slow when k is large.

Output: a single sorted sequence of all elements from the k arrays.

Example

Input:

  • k = 3
  • arrays[0] = [1, 4, 9]
  • arrays[1] = [2, 6]
  • arrays[2] = [0, 3, 7, 8]

Output:

  • [0, 1, 2, 3, 4, 6, 7, 8, 9]

Constraints

  • 1 <= k <= 10^5
  • Each array length can be zero or more.
  • Total number of elements N across all arrays: 0 <= N <= 10^6 (assume this upper bound for complexity discussion).
  • Integer values fit in 32-bit signed range.

Requirements

  1. Describe an efficient algorithm for this problem, including:
    • Data structures you would use.
    • Time and space complexity.
  2. Then implement the function that merges the arrays following your algorithm.

(Hint: Consider a priority-based data structure that efficiently gives you the smallest current element among the k arrays.)

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
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.