Deep Copy a Random-Pointer List
Updated
Problem statement
A linked list has n nodes in next-pointer order. Each node contains an integer value and a random pointer that may target any node or be null.
The list is encoded by nodes, where nodes[i] = [value, randomIndex]. The next pointer of node i targets node i + 1, except for the last node. A randomIndex of -1 means null.
Create a deep copy whose next and random pointers target only copied nodes. For this exercise, after copying, mutate the original node at mutationIndex: set its value to newValue and its random pointer to newRandomIndex.
Return two encoded snapshots in order: the mutated original list, then the unchanged deep copy.
Function
copyAndMutateRandomList(nodes: int[][], mutationIndex: int, newValue: int, newRandomIndex: int) → int[][][]Examples
Example 1
nodes = [[7,-1],[13,0],[11,4],[10,2],[1,0]]mutationIndex = 1newValue = 130newRandomIndex = 4return = [[[7,-1],[130,4],[11,4],[10,2],[1,0]],[[7,-1],[13,0],[11,4],[10,2],[1,0]]]The second original node changes to [130,4]. The second copied node remains [13,0], so the copy is independent.
Example 2
nodes = [[1,1],[2,1]]mutationIndex = 0newValue = 10newRandomIndex = -1return = [[[10,-1],[2,1]],[[1,1],[2,1]]]The first original node loses its random pointer. The copy retains both original random references.
Example 3
nodes = [[5,0]]mutationIndex = 0newValue = 6newRandomIndex = -1return = [[[6,-1]],[[5,0]]]The one-node copy keeps its self-random pointer even after the original is changed.
Constraints
1 <= nodes.length <= 10^4.- Every row of
nodescontains exactly two integers. -10^9 <= nodes[i][0], newValue <= 10^9.- Every random index and
newRandomIndexis-1or an index from0throughnodes.length - 1. 0 <= mutationIndex < nodes.length.
More Amazon Onsite Interview questions
- Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dungeon GameONSITE INTERVIEW · Seen Nov 2026
- Integer BreakONSITE INTERVIEW · Seen Nov 2026
- LFU CacheONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Meta Copy List with Random PointerMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Deep Copy a Random-Pointer ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Deep Copy a Random-Pointer ListSuperhuman · PHONE SCREENSuperhuman interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Propagating Fraud DetectorAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Instant-Runoff Voting WinnerWise · ONSITE INTERVIEW
- First-Fit Memory AllocatorGoogle · ONSITE INTERVIEWGoogle interview questions
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- LRU CacheAMD · ONSITE INTERVIEWAMD interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
Amazon interview experiences
- Amazon SWE OA with AI Coding: 1 Experience
- Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
- Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
- Amazon BIE Intern SQL and Interview Questions: 17 Experiences
- Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
- Amazon SDE II (L5) Interview Questions: 5 Experiences