mediumGraphsFacebookGooglePocket Gems
Social Graph Mirror
A privacy pipeline deep-copies an entire friends-of-friends graph (preserving cycles) before sharing with downstream analytics.
Given a reference of a node in a connected undirected graph, return a deep copy of the graph.
Choose approach
1
cur
2
3
4
1/5
Narrationstep 1 of 5
Clone node 1; store original→copy mapping.
Variables
node=1
cloned=1
How to think about it
- Create the copy BEFORE recursing to its neighbors (prevents cycles).
- Map lookups make the neighbor hookup O(1).
- BFS variant is equivalent; same O(V+E) cost.
Edge cases to catch in interview
- Null input -> null.
- Disconnected graph: only clone the reachable component.
Examples
Example 1
cloned 4 nodes{"adj":[[1,3],[0,2],[1,3],[0,2]],"start":0}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed