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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

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
Linked list
Hash map
Deep copy
Pointer manipulation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported