Clone a Linked List with Random Pointers

Asked atApple
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
  1. 01Explain the map solution first.
  2. 02Map every original node to a clone.
  3. 03Wire next and random through the map.
  4. 04Optionally interleave clones for O(1) extra space.
  5. 05Separate the two lists carefully.

Reference answer

Then expect these follow-ups

  • Why does the interleaving trick avoid a map?

    Tests: correctness reasoning

  • How would this generalize to an arbitrary graph?

    Tests: follow-up reasoning

Hiring now

Get the Apple interview, not just the prep

landed applies to roles like these for you, on each company’s own site, with answers written for the role.

Get 5x interviews10 free auto-applies. No card.
Or practice this question first

Next question