Deep Copy a Story Chain Where Each Node Has a Random Link
Last updated: November 29, 2026
Quick Overview
Each node in a linked chain of stories has a next pointer and a random pointer to any node in the chain or None. Return a deep copy whose next and random pointers reference only new nodes.
Meta
November 29, 20260
0
0 solved
Each node in a linked chain of stories has a next pointer and a random pointer to any node in the chain or None. Return a deep copy whose next and random pointers reference only new nodes.
A Meta phone-screen staple framed as cloning a chain of Stories where each one may reference another (for example a reshare). The interviewer is looking for a correct hash map solution quickly and is impressed by the O(1) extra space interleaving trick.
What the Interviewer Expects
- Explain why a shallow copy is wrong
- Give a two-pass hash map solution from old node to new node
- Present the interleaving approach with O(1) extra space
- Restore the original list after splitting
- Handle random pointers that are None or point to self
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
- Copy a general graph where each node has a list of neighbors.
- What if the structure may contain a cycle through next pointers?
- How would you serialize this structure to send over the network?
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 Node(val, next, random). Output: head of a new list with identical values and identical structure, where no pointer in the copy references an original node. The original lis...
High-Level Design
Hash map version: first pass creates a copy of each node and stores old -> new; second pass sets new.next = map[old.next] and new.random = map[old.random]. That is O(n) space. The interleaving v...