FrontendInterviews.dev

Loading problem…

287. Merge K Sorted Lists

Hard•
Acceptance: 80.00%
•
🔓3/3 Pro unlocks today

Given k sorted lists (arrays), merge them into one globally sorted array.

A brute-force approach is to flatten everything and sort once, but interviewers usually expect a better merge strategy that uses the fact each list is already sorted.

Requirements

Implement:

function mergeKSortedLists(lists) {}

Behavior expectations:

  • Handle empty input and empty inner lists
  • Preserve duplicates
  • Support negative and positive numbers
  • Return deterministic sorted output

Example Usage

mergeKSortedLists([
  [1, 4, 5],
  [1, 3, 4],
  [2, 6],
]); // [1, 1, 2, 3, 4, 4, 5, 6]

mergeKSortedLists([]); // []

Constraints

  • 0 <= k <= 10^4
  • Total element count can be large
  • Prefer better than flatten+global sort for interview quality