Deep Copy a Service Dependency Graph Including Its Cycles

Last updated: November 29, 2026

Quick Overview

Given a reference to one node of a connected undirected graph where each node has a value and a list of neighbors, return a deep copy of the whole graph that preserves its structure, including cycles.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a reference to one node of a connected undirected graph where each node has a value and a list of neighbors, return a deep copy of the whole graph that preserves its structure, including cycles.

A technical screen question about copying an in-memory topology, for example snapshotting a service graph for a what-if simulation. The interviewer checks that you map old nodes to new nodes so cycles do not loop forever and shared neighbors are not duplicated.

What the Interviewer Expects
  • Keep a hash map from original node to its clone
  • Create clones on first discovery
  • Wire neighbor lists using the map
  • Traverse with BFS or DFS
  • Handle a null input and self-loops
Key Topics to Cover
graph cloning
hash map
BFS
cycles
deep copy
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you serialize the graph so it can be sent over the network?
  • What if the graph is not connected and you get a list of nodes?
  • How would you clone a graph with random pointers, like a linked list with random links?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: a node GraphNode(val, neighbors) or None.
  • Output: the clone of that node, with all reachable nodes cloned.
  • Clones must be new objects; no clone may point to an original....
High-Level Design

The map old -> new is the whole idea. It acts as the visited set, guarantees one clone per original so shared neighbors stay shared, and makes cycles safe because an already-cloned node is returned ...


Submit Your Answer
Markdown supported