Given k sorted linked lists, merge them into a single sorted linked list and return the merged list.

What the interviewer wants to learn

  • Your ability to work with linked‑list data structures.
  • How you choose an algorithm that balances time and space efficiency.
  • Whether you can explain the approach clearly and reason about its complexity.
  • Your coding style: readability, handling edge cases, and testing.

How to structure your answer

1. **Restate the problem** briefly to confirm understanding.

2. **Discuss possible strategies** (e.g., naïve concatenation, pairwise merging, priority queue) and why you pick one.

3. **Explain the algorithm step by step**, covering:

  • Initialization (dummy head, min‑heap or pointers).
  • Main loop condition.
  • How you advance pointers and maintain ordering.

4. **State time and space complexity** and compare it to alternatives.

5. **Mention edge cases** (empty input, single list, duplicate values).

6. **Write the code** (concise, well‑named variables, comments).

7. **Briefly describe a quick test** to prove correctness.

Example answer

“Sure, the task is to take *k* already‑sorted singly linked lists and produce one sorted list that contains all the nodes. A straightforward way would be to concatenate all lists and then sort, but that would be O(N log N) where N is the total number of nodes. A better approach is to always pull the smallest current node from the *k* heads.

I’ll use a min‑heap (priority queue) that stores a tuple (value, list_index, node). First, I push the head of every non‑empty list onto the heap. Then I repeatedly pop the smallest element, attach it to a dummy result list, and if the popped node has a next node I push that next node onto the heap. The loop stops when the heap is empty, meaning all nodes have been merged.

Because each node is pushed and popped exactly once, the time complexity is O(N log k) and the extra space is O(k) for the heap. This beats the naïve O(N log N) solution, especially when k is much smaller than N.

Edge cases: if the input array is empty or all lists are empty, I just return None. Duplicate values work automatically because the heap orders by value.

Here’s the Python implementation:

import heapq
from typing import List, Optional

class ListNode:
    def __init__(self, val: int, next: Optional['ListNode'] = None):
        self.val = val
        self.next = next

def merge_k_lists(lists: List[Optional[ListNode]]) -> Optional[ListNode]:
    heap = []
    for i, node in enumerate(lists):
        if node:
            heapq.heappush(heap, (node.val, i, node))

    dummy = tail = ListNode(0)
    while heap:
        val, i, node = heapq.heappop(heap)
        tail.next = node
        tail = tail.next
        if node.next:
            heapq.heappush(heap, (node.next.val, i, node.next))
    return dummy.next

I’d test it with three short lists like `[1→4→5]`, `[1→3→4]`, and `[2→6]` and verify the output is `1→1→2→3→4→4→5→6`.

That’s the whole solution.”

Mistakes to avoid

  • **Skipping complexity analysis** or giving a vague “fast” claim without numbers.
  • **Choosing the naïve O(N log N) method** when a better O(N log k) solution exists, unless you explicitly explain why you’d accept the trade‑off.
  • **Forgetting to handle empty inputs** (null list or empty array).
  • **Modifying input nodes incorrectly**, e.g., losing references or creating cycles.
  • **Writing a heap implementation from scratch** in a language that already provides one, which wastes time and introduces bugs.
  • **Leaving out a test case**; interviewers expect you to sanity‑check your code.