PROBLEM 41 OF 75

Clone Graph

Create a deep copy of every node and edge reachable from a start node.

PATTERNGraphs: traversal & orderingNodes are facts; edges are allowed moves.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Create a deep copy of every node and edge reachable from a start node.

WHY THIS FITS

Cycles require remembering a clone before exploring neighbors.

COMPLEXITY

O(V + E) time · O(V) space

COMMON FAILURE

Creating the clone after recursion loops forever on cycles.

THE REASONING, IN ONE PLACE

How to solve Clone Graph

The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.

What the question asks

Create a deep copy of every node and edge reachable from a start node.

Why Graphs: traversal & ordering fits

Cycles require remembering a clone before exploring neighbors.

State to maintain

A map from original node identity to clone.

Transition

Create/store a clone, recursively clone neighbors, and reuse mapped clones.

Time and space

O(V + E) time · O(V) space

Counterexample to the tempting mistake

Creating the clone after recursion loops forever on cycles.

Prove it again tomorrow

Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.

Open blank recall ↗