Deep Copy a Random-Pointer List
Updated
Problem statement
Given a linked list of n nodes stored in nodes, make a deep copy of it. Then change one node of the original and return both lists.
Row nodes[i] = [value, randomIndex] describes node i, which holds an integer value. Its next pointer goes to node i + 1, and the last node has no next node. Its random pointer goes to node randomIndex, which may be any node, or is null when randomIndex is -1.
A deep copy uses only new nodes. Its next and random pointers point only to copied nodes. After copying, change the original node at mutationIndex: set its value to newValue and its random pointer to newRandomIndex.
Return two lists in the same row form: first the changed original, then the deep copy, which must stay unchanged.
Follow-up: with real pointer nodes, make the deep copy using O(1) extra space besides the copied nodes and the returned lists. You may link copied nodes in between the original nodes for a while, but you must restore the original next pointers before returning.
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 is still [13,0], so the copy does not depend on the original.
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. Both copied nodes still keep their original random pointers.
Example 3
nodes = [[5,0]]mutationIndex = 0newValue = 6newRandomIndex = -1return = [[[6,-1]],[[5,0]]]The one-node copy keeps its random pointer to itself, even after the original changes.
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 Bloomberg LP Phone Screen questions
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Deep Copy a Random-Pointer ListAmazon · ONSITE INTERVIEW
- Meta Copy List with Random PointerMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Deep Copy a Random-Pointer ListSuperhuman · PHONE SCREENSuperhuman interview questions
- Sustained Overspeed AlertsFleetx · ONSITE INTERVIEW
- LRU CacheAthenahealth · PHONE SCREEN
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- Logger Rate Limiter with Every-Arrival HistoryGoogle · PHONE SCREENGoogle interview questions
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)