FastPrepDeep Copy a Random-Pointer List
Problems/Deep Copy a Random-Pointer List

Deep Copy a Random-Pointer List

Snowflake logoSnowflake● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Our version is about 90% the same as the reported question, which is about making a deep copy while keeping the same links. We chose how nodes are numbered in the input, fresh labels for the copies that come out the same each time, how to write null, the limits, the examples and the function signature. This exercise comes from a collection of interview-prep questions, not from one dated interview.

The table nodes describes a singly linked list. Each row is [id, value, nextId, randomId]. Every node has its own positive integer ID, and 0 stands for a null reference, meaning a reference to no node. Rows can come in any order. head is the ID of the first node.

Start at the head and follow the next references. This path is the next chain: it visits every node exactly once and then reaches 0. A random reference may be null, or it may point to any node in this same list, including the node itself. Random references may form cycles. They do not change the order of the next chain.

Make a deep copy of the list. That means you build new nodes instead of reusing the old ones. Each copied node keeps the value and both references of its original, but gets a new ID. Each non-null reference in the copy must point to a copied node, using that node's new ID.

Return the copied list as a table, built like this:

  • Walk the original list in next-chain order. The copy of the first node gets ID -1, the copy of the second gets -2, and so on.
  • List the rows in that same order. Each row is [copiedId, value, copiedNextId, copiedRandomId].
  • Replace each original next or random target with the new ID of its copy. A null reference stays 0.

If the original list is empty, nodes is empty and head is 0. In that case, return an empty table. Do not change the input table. Different nodes may have the same value, so use their IDs to tell them apart.

Function

copyRandomList(nodes: int[][], head: int) → int[][]

Examples

Example 1

nodes = [[17,9,0,42],[42,7,8,17],[8,7,17,42]]head = 42return = [[-1,7,-2,-3],[-2,7,-3,-1],[-3,9,0,-1]]

Following the next references from the head gives 42, 8, 17, even though the rows come in a different order. Their copies get IDs -1, -2, -3. So the random references to 17 and 42 become -3 and -1, the IDs of their copies. Two nodes have the same value, so the IDs, not the values, show which node a reference points to.

Example 2

nodes = [[5,-2,0,5]]head = 5return = [[-1,-2,0,-1]]

The one copied node has ID -1. Its random reference points back to that copied node, not to original ID 5.

Example 3

nodes = []head = 0return = []

An empty original list has no copied nodes, so the returned table is empty.

Constraints

  • 0 <= nodes.length <= 1000, and every row has exactly four integers.
  • Original IDs are unique, with 1 <= id <= 10^9.
  • Each value satisfies -10^9 <= value <= 10^9.
  • head == 0 exactly when the table is empty. Otherwise, head is the ID of a node in the table.
  • Every non-null next or random target is an original ID that exists in the table.
  • The next chain from head is finite, never repeats a node, includes every row, and ends at zero.
  • When the list is not empty, copied IDs go from -nodes.length through -1. Zero means null for both original and copied IDs.

More Snowflake Phone Screen questions

  • Minimum StackPHONE SCREEN · Seen Nov 2026
  • Design In-Memory File SystemPHONE SCREEN · Seen Nov 2026
  • Merge Two Binary TreesPHONE SCREEN · Seen Nov 2026
  • Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
  • Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
  • Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
  • Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
  • Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
  • Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • LRU CacheAthenahealth · PHONE SCREEN
  • LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions
  • Balanced Session ManagerPalantir · PHONE SCREENPalantir interview questions
  • LRU CacheGimlet Labs · PHONE SCREEN
  • LRU CacheBNSF Railway · PHONE SCREENBNSF Railway interview questions
  • Max Frequency TrackerProcol · PHONE SCREEN
  • LRU Cache Operation SequenceCARS24 · PHONE SCREEN
  • LRU Key-Value Cache OperationsCohesity · PHONE SCREEN

Snowflake interview experiences

  • Snowflake AI Coding Interview, Experienced Hires: 3 Experiences
All Snowflake interview questionsSee Snowflake hiring insights
Saved
class Solution {
    public int[][] copyRandomList(int[][] nodes, int head) {
        // Write your code here.
    }
}
Checking account…