Deep Copy a Linked List of Docs Comments With Reply Pointers
Last updated: November 29, 2026
Quick Overview
Each node in a list of Google Docs comments has a next pointer and a reply_to pointer that may point to any comment in the list or None. Produce a deep copy where no copied pointer refers to an original node.
0
0
0 solved
Each node in a list of Google Docs comments has a next pointer and a reply_to pointer that may point to any comment in the list or None. Produce a deep copy where no copied pointer refers to an original node.
A common Google onsite question that tests reasoning about object identity and mapping old nodes to new ones. The interviewer usually accepts the hash map solution first and then asks for O(1) extra space via interleaving.
What the Interviewer Expects
- Build a mapping from original to copy (hash map) as the first solution
- Wire next and reply_to through the mapping in a second pass
- Offer the interleaving trick for O(1) extra space
- Restore the original list after interleaving
- Handle reply_to pointing to itself or to None
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Do it in O(1) extra space.
- Comments form a general graph with many pointer fields. Generalize.
- How would you copy this list across a network boundary (serialization)?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: head of a list of nodes with
val,next,reply. - Output: head of an independent copy with identical structure.
- Originals must be unchanged afterward....
High-Level Design
The challenge is that reply can point forward, so a copy might not exist yet when you need it. Hash map solution: pass 1 creates a copy for every node and records old -> new; pass 2 sets `new.next...