Find where two singly linked lists merge, given each as a sequence of node ids

Read the full interview experience this question came from →

Quick Overview

Coding practice question: two singly linked lists that may merge into a shared tail are given as sequences of node ids, and you must return the id of the node where they first meet, or -1. It tests reasoning about linked list structure, linear-time traversal, and edge cases such as no shared node or one list being the other's tail.

Find where two singly linked lists merge, given each as a sequence of node ids

Company: Microsoft

Role: Site Reliability Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

Two singly linked lists can merge: from some node onward they share the very same nodes, so together they form a Y shape. You are given each list as the sequence of node ids met while walking it from its head to its tail. Return the id of the node at which the two lists merge, or `-1` if they share no node. ### Function Signature ```python def first_shared_node(list_a: list[int], list_b: list[int]) -> int: ``` ### Rules - A node id identifies a node, not a value: the two lists share a node exactly when the same id appears in both. - Every node has a single `next` pointer, so once both walks reach a shared node they visit identical nodes from there to the common tail. - Inputs are consistent: ids are distinct within each list, and if the lists share any node, the suffix of `list_a` that starts at the first shared id is identical to the suffix of `list_b` that starts at the same id. No id outside that common suffix appears in both lists. - Return the first shared id in walking order (the merge node). When it exists it is unique. - Do not modify the inputs. ### Constraints - `1 <= len(list_a) <= 3 * 10^4` - `1 <= len(list_b) <= 3 * 10^4` - `1 <= list_a[i], list_b[j] <= 10^9` (fits in a signed 32-bit integer) - The return value is either an id present in both lists or `-1`. ### Examples Example 1 ```text list_a = [11, 12, 30, 31, 32] list_b = [21, 22, 23, 30, 31, 32] output = 30 ``` The lists merge at node 30 and then share nodes 30, 31 and 32. Example 2 ```text list_a = [1, 2, 3] list_b = [4, 5] output = -1 ``` No node is shared. Example 3 ```text list_a = [5] list_b = [9, 5] output = 5 ``` All of `list_a` is the tail of `list_b`, so the merge node is `list_a`'s head.

Overview: Coding practice question: two singly linked lists that may merge into a shared tail are given as sequences of node ids, and you must return the id of the node where they first meet, or -1. It tests reasoning about linked list structure, linear-time traversal, and edge cases such as no shared node or one list being the other's tail.

Read the full Microsoft Site Reliability Engineer interview experience this question came from

|Home/Coding & Algorithms/Microsoft
Microsoft logo
Microsoft
Jul 30, 2026
mediumSite Reliability EngineerOnsiteCoding & Algorithms
0
0

Two singly linked lists can merge: from some node onward they share the very same nodes, so together they form a Y shape. You are given each list as the sequence of node ids met while walking it from its head to its tail. Return the id of the node at which the two lists merge, or -1 if they share no node.

Function Signature

def first_shared_node(list_a: list[int], list_b: list[int]) -> int:

Rules

  • A node id identifies a node, not a value: the two lists share a node exactly when the same id appears in both.
  • Every node has a single next pointer, so once both walks reach a shared node they visit identical nodes from there to the common tail.
  • Inputs are consistent: ids are distinct within each list, and if the lists share any node, the suffix of list_a that starts at the first shared id is identical to the suffix of list_b that starts at the same id. No id outside that common suffix appears in both lists.
  • Return the first shared id in walking order (the merge node). When it exists it is unique.
  • Do not modify the inputs.

Constraints

  • 1 <= len(list_a) <= 3 * 10^4
  • 1 <= len(list_b) <= 3 * 10^4
  • 1 <= list_a[i], list_b[j] <= 10^9 (fits in a signed 32-bit integer)
  • The return value is either an id present in both lists or -1 .

Examples

Example 1

list_a = [11, 12, 30, 31, 32]
list_b = [21, 22, 23, 30, 31, 32]
output = 30

The lists merge at node 30 and then share nodes 30, 31 and 32.

Example 2

list_a = [1, 2, 3]
list_b = [4, 5]
output = -1

No node is shared.

Example 3

list_a = [5]
list_b = [9, 5]
output = 5

All of list_a is the tail of list_b, so the merge node is list_a's head.