L2 · Working engineerLists & arrays~8 min · 5 tests#12

Merge Two Sorted Lists

Merge two sorted Python lists into one sorted list with two pointers in O(n+m) — no sort(). Test duplicates and empty inputs in your browser.

The problem

list1 and list2 are each sorted ascending. Return one sorted list containing every element of both.

Do it without calling sorted() or .sort().

Examples

  1. Example 1

    Input

    merge_sorted([1, 3, 5, 7], [2, 4, 6, 8])

    Expected output

    [1, 2, 3, 4, 5, 6, 7, 8]
  2. Example 2

    Input

    merge_sorted([1, 2, 2], [2, 3])

    Expected output

    [1, 2, 2, 2, 3]

+ 3 hidden tests on Submit — one list entirely smaller.

Edge cases to ask about

  • One list empty
  • Duplicates across lists
  • One list entirely before the other

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · merge_sorted
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    Concatenate and sortO((n+m) log(n+m))O(n+m)Ignores the fact that both inputs are already sorted.
    bestTwo pointersO(n + m)O(n + m)Always take the smaller head; append the leftovers at the end.
    Walkthrough of the optimal approach (try it yourself first)

    Hold an index i into list1 and j into list2. Repeatedly append the smaller head and advance that index. When one side is exhausted, extend with the other's remainder.

    This is the merge step of merge sort, and the same idea as heapq.merge — mention that for the follow-up about k lists.

    Complexity: O(n + m) time, O(n + m) space. Each element is appended exactly once; the output holds all n + m elements.

    Reveal the reference solution
    def merge_sorted(list1, list2):
        i = j = 0
        out = []
        while i < len(list1) and j < len(list2):
            if list1[i] <= list2[j]:
                out.append(list1[i]); i += 1
            else:
                out.append(list2[j]); j += 1
        out.extend(list1[i:])
        out.extend(list2[j:])
        return out

    Follow-ups interviewers ask

    • Merge k sorted lists (heap, O(N log k)).
    • Merge in place into list1 with spare capacity at its end.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Merge Two Sorted Lists in Python?

    The optimal solution runs in O(n + m) time and O(n + m) auxiliary space. Each element is appended exactly once; the output holds all n + m elements.

    What is the brute-force approach, and how do you optimise it?

    Concatenate and sort: O((n+m) log(n+m)) time, O(n+m) space. Ignores the fact that both inputs are already sorted. Two pointers: O(n + m) time, O(n + m) space. Always take the smaller head; append the leftovers at the end.

    What follow-up questions do interviewers ask about Merge Two Sorted Lists?

    Merge k sorted lists (heap, O(N log k)). Merge in place into list1 with spare capacity at its end.