Deep Copy a Linked List With Random Pointers
Updated
Problem statement
Make a deep copy of a linked list whose nodes carry a next pointer and a random pointer, then report what the copy looks like. Every node of the copy must be new, and every pointer in the copy must lead to a node of the copy.
How the list is given: nodes has one row per node, [nodeId, value, nextNodeId, randomNodeId], and head is the id of the first node. In Example 1, the row [40, 3, 7, 19] says that node 40 holds 3, its next node is node 7, and its random pointer leads to node 19; the row [7, -1, 19, 7] has a random pointer that leads back to node 7 itself. A pointer id of -1 means null, and the rows can appear in any order.
How to report: walk the copy from its first node and write one row per node, [value, randomPosition]. randomPosition is the 0-based place in the list of the node the random pointer leads to, or -1 when it is null. If nodes is empty, head is -1 and the report is empty.
Function
copyRandomList(nodes: int[][], head: int) → int[][]Examples
Example 1
nodes = [[7,-1,19,7],[19,8,-1,-1],[40,3,7,19]]head = 40return = [[3,2],[-1,1],[8,-1]]Start at id 40 and follow next: 40 → 7 → 19, so the list is 3, -1, 8. Node 40 points at 19, the third node (position 2). Node 7 points at itself, position 1. Node 19 has no random pointer.
Example 2
nodes = []head = -1return = []There are no nodes and head is -1, so the copy is empty.
Example 3
nodes = [[2,6,-1,2],[9,6,2,2]]head = 9return = [[6,1],[6,1]]The list is 9 → 2. Both nodes hold 6, but they are different nodes, and both random pointers reach node 2, the second one. So both rows end in 1.
Constraints
nodeshas at most1000rows.- Ids are distinct integers from
0to10^6, and-10^4 <= value <= 10^4. - The rows form exactly one chain: every row lies on the path that starts at
headand follows the next ids, and that path ends in-1. EachrandomNodeIdis-1or the id of some row.
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
- LRU CacheLotusFlare · ONSITE INTERVIEW
- LRU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- LRU Cache OperationsHevo Data · ONSITE INTERVIEW
- LRU Cache OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- 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
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences