Deep Copy an Order Chain With Random Referral Pointers

Last updated: November 29, 2026

Quick Overview

Orders form a singly linked chain via `next`, and each order also has a `referral` pointer to any order in the chain or None. Produce a deep copy of the whole chain where every pointer in the copy refers only to copied nodes.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Orders form a singly linked chain via `next`, and each order also has a `referral` pointer to any order in the chain or None. Produce a deep copy of the whole chain where every pointer in the copy refers only to copied nodes.

A pointer manipulation problem common in Amazon onsites. The interviewer checks that you can handle arbitrary cross references without double-copying, first with a hash map and then, if pushed, with the O(1) extra space interleaving trick.

What the Interviewer Expects
  • Map each original node to its copy
  • Wire next and referral pointers in a second pass
  • Offer the interleaving approach for O(1) extra space
  • Restore the original list after interleaving
  • Handle None referrals and self references
Key Topics to Cover
Linked Lists
Hash Map
Deep Copy
Pointers
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
  • How would you copy a general graph of orders with cycles?
  • How would you verify the copy shares no nodes with the original?
  • What changes if referrals can point outside the chain?
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

Head may be None. Referral may be None or point to any node, including itself. The original list must be unchanged after the call....

High-Level Design

Hash map approach: pass 1 creates a copy of each node and stores orig -> copy. Pass 2 sets copy.next = map[orig.next] and copy.referral = map[orig.referral], mapping None to None. O(n) extra spa...


Submit Your Answer
Markdown supported