← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Google SWE coding round, one linked list problem that looks straightforward until you hit the follow-up about doing it in constant space.

Questions Asked (1)

Q1

Given two singly linked lists that may merge at some point, find the first node they share. The lists can be different lengths, and intersection means same reference, not same value. Follow-up: can you do it in O(m+n) time and O(1) space?

Algorithms & Data Structures
Author's notes

My first instinct was a hash set, which works fine but fails the space constraint.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then present a straightforward solution using hash sets to achieve O(m+n) time and O(m) space. For the follow-up, explain the two-pointer technique that aligns list lengths by switching pointers, achieving O(m+n) time and O(1) space.

Pro tip: Emphasize that intersection is by reference, not value, and proactively discuss edge cases like no intersection or one list being empty. This shows attention to detail and prevents incorrect assumptions.

1. Clarify the problem

Confirm that intersection means the same node reference, not just equal values. Ask about edge cases: empty lists, no intersection, or one list being a prefix of the other.

2. Present a baseline solution

Propose using a hash set to store nodes of one list, then traverse the other to find the first common node. This takes O(m+n) time and O(m) space.

3. Optimize to O(1) space

Introduce the two-pointer technique: traverse both lists simultaneously, and when a pointer reaches the end, redirect it to the head of the other list. They will meet at the intersection or both become null.

4. Analyze complexity and edge cases

Explain that the two-pointer approach runs in O(m+n) time and O(1) space. Discuss how it handles different lengths and no intersection (both pointers become null after at most m+n steps).

5. Test with examples

Walk through a concrete example with lists of different lengths and an intersection, and another with no intersection, to verify the logic.

Key Points to Mention

  • Intersection is defined by node reference, not value.
  • Hash set solution: O(m+n) time, O(m) space.
  • Two-pointer solution: O(m+n) time, O(1) space.
  • Handling different lengths by switching pointers to the other list's head.
  • Edge cases: no intersection, empty lists, one list empty.
  • Proof of correctness: pointers traverse equal total distance (m+n) before meeting or becoming null.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.