FastPrepCopy a Random-Pointer List in Constant Space
Problems/Copy a Random-Pointer List in Constant Space

Copy a Random-Pointer List in Constant Space

Apple logoApple● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple full-time onsite interview. The report requires the constant-space solution that weaves the copies into the original list, and it requires that the input list is put back exactly. Here the list comes as a matrix. After copying, a test changes things so we can see that the input was restored and that the clone is independent.

A linked list has one node per row of nodes. Row i is [value, randomIndex]:

  • The node's next pointer goes to row i + 1, or is null for the final row.
  • randomIndex is the row index reached by its random pointer, or -1 for null.

Build the list, create a deep copy, and separate the two lists. The copy phase must use O(1) auxiliary space beyond the newly allocated clone nodes by interleaving each clone immediately after its original node. Restore every original next pointer exactly, wire every clone's next and random pointers to clone nodes, and null-terminate the clone tail.

Immediately after separation, serialize the restored original. Then, when the list is non-empty, change the original node at mutationIndex to newValue and redirect its random pointer to newRandomIndex, where -1 means null. Return [restoredOriginal, mutatedOriginal, deepCopy]. The copy must retain the pre-mutation values and random links. For an empty list, mutationIndex and newRandomIndex are -1 and all three snapshots are empty. The storage used to read the input and the returned snapshots do not count toward the copy phase's extra-space limit.

Function

copyRandomListConstantSpace(nodes: int[][], mutationIndex: int, newValue: int, newRandomIndex: int) → int[][][]

Examples

Example 1

nodes = [[7,-1],[13,0],[11,4],[10,2],[1,0]]mutationIndex = 2newValue = 99newRandomIndex = 1return = [[[7,-1],[13,0],[11,4],[10,2],[1,0]],[[7,-1],[13,0],[99,1],[10,2],[1,0]],[[7,-1],[13,0],[11,4],[10,2],[1,0]]]

The first snapshot shows that the original list was restored. Changing original node 2 changes only the middle snapshot. The clone keeps value 11 and random target 4.

Example 2

nodes = []mutationIndex = -1newValue = 0newRandomIndex = -1return = [[],[],[]]

An empty list has empty restored, mutated, and clone snapshots.

Example 3

nodes = [[5,0],[5,0]]mutationIndex = 0newValue = 8newRandomIndex = 1return = [[[5,0],[5,0]],[[8,1],[5,0]],[[5,0],[5,0]]]

Equal values do not identify nodes. The change redirects only the original first node to row 1. The clone's random links still point to clone row 0.

Constraints

  • 0 <= nodes.length <= 100000.
  • Every row contains exactly [value, randomIndex].
  • All node values and newValue fit a signed 32-bit integer.
  • Every stored random index and newRandomIndex is -1 or a valid row index.
  • For a non-empty list, 0 <= mutationIndex < nodes.length. For an empty list, mutationIndex == -1.
  • The clone phase must use O(1) auxiliary space beyond clone nodes and must not use a map from original nodes to clones.
  • The returned snapshots and the input storage do not count toward the extra-space limit of the clone phase.

More Apple Onsite Interview questions

  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
  • Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • FIFO Mutex Operation TraceONSITE INTERVIEW · Seen Oct 2026
  • Implement Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Parse Nested HTML into a TreeONSITE INTERVIEW · Seen Oct 2026
  • Thread-Safe Concurrent Linked ListONSITE INTERVIEW · Seen Sep 2026
  • Palindrome Match PercentageONSITE INTERVIEW · Seen Sep 2026
  • Binary Tree Nodes at Distance KONSITE INTERVIEW · Seen Aug 2026
  • BST to Sorted Circular Doubly Linked ListONSITE INTERVIEW · Seen Aug 2026

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
  • Least Recently Used CacheByteDance · PHONE SCREEN · ONSITE INTERVIEWByteDance interview questions

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple India SWE New Grad Interviews: 5 Experiences
  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public int[][][] copyRandomListConstantSpace(int[][] nodes, int mutationIndex, int newValue, int newRandomIndex) {
        // Write your code here.
    }
}
Checking account…