Merge Two Sorted Lists
Python data engineering interview problem. Difficulty: intermediate. Pattern: Linked Lists. About 14 minutes. Part of the Pro drill bank.
Merge two sorted value-lists into one sorted list. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Two services each produce an already sorted stream of ids. Combining them should keep the order and keep duplicate ids when both streams contain one. Merge the two ascending lists into one ascending list. Take the smaller current value, advance that list, and append the remaining tail when one list ends. Write merge_two_lists(a: list, b: list) -> list. [1, 2, 4] and [1, 3, 4] become [1, 1, 2, 3, 4, 4]. The starter file already has the function name and harness print lines. Keep them and fill only the function body.
Requirements
- Keep every value from both lists.
- Return ascending order.
- Keep equal values from both inputs.
Constraints
- Both inputs are already sorted and either may be empty.
- Return a new list with integer or comparable values.
Examples
Input: merge_two_lists([1, 2, 4], [1, 3, 4]) Output: [1, 1, 2, 3, 4, 4] Stable merge of both chains.
Topics: lakebench, python, merge, list api.
More Python interview questions · All interview problems · Learn data engineering
Merge Two Sorted Lists
Interview-style drill: Merge two sorted value-lists into one sorted list.
Implement `merge_two_lists(a: list, b: list) -> list` merging ascending sorted lists (list API). Example: `[1, 2, 4], [1, 3, 4]` → `[1, 1, 2, 3, 4, 4]`. Keep the harness.