Clone Graph

Data Structures
Medium
Salesforce
71K views

Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. Use a HashMap to track visited nodes and their clones (BFS or DFS).

Why Interviewers Ask This

Salesforce interviewers ask this to evaluate a candidate's mastery of graph traversal algorithms and memory management. They specifically test your ability to handle cycles in undirected graphs without creating infinite loops, while ensuring a true deep copy rather than a shallow reference. This assesses your understanding of HashMap usage for state tracking and your capacity to write clean, production-ready code under pressure.

How to Answer This Question

1. Clarify the input: Confirm if the graph is connected, directed, or contains self-loops, as Salesforce values precise problem definition. 2. Choose a traversal strategy: Explicitly decide between Breadth-First Search (BFS) using a Queue or Depth-First Search (DFS) using recursion, explaining why one might be safer against stack overflow for large inputs. 3. Initialize data structures: Propose a HashMap where keys are original nodes and values are their cloned counterparts to prevent re-cloning and handle cycles. 4. Execute traversal: Walk through the algorithm step-by-step, detailing how you create a new node only if it hasn't been visited, then recursively or iteratively add neighbors to the clone. 5. Validate edge cases: Mention handling null inputs or single-node graphs before returning the final cloned head.

Key Points to Cover

  • Explicitly mention using a HashMap to track visited nodes and prevent infinite loops caused by cycles
  • Demonstrate clear distinction between shallow copy (reference) and deep copy (new objects)
  • Select BFS or DFS with a reasoned justification regarding memory safety or implementation complexity
  • Handle edge cases like null inputs or single-node graphs proactively
  • Explain the logic for linking neighbors in the cloned graph during traversal

Sample Answer

To solve the Clone Graph problem efficiently, I would first clarify that we are dealing with an undirected graph which inherently contains cycles. My approach uses a Hash Map to map every original node to its corresponding clone, ensuring we never process the same node twice. I prefer using Breadth-First Search (BFS) here because it naturally handles level-by-level copying and avoids potential stack overflow issues that could occur with deep recursive DFS on large Salesforce-scale datasets. I will initialize a queue and a map. First, I check if the input node is null; if so, return null immediately. Otherwise, I create a clone of the starting node, store it in the map, and push the original into the queue. While the queue is not empty, I dequeue a current node. For each neighbor of this current node, I check if it exists in our map. If it does, I simply append that existing clone to the current clone's neighbor list. If it doesn't exist, I create a fresh clone, add it to the map, enqueue the original neighbor, and append the new clone to the current clone's list. This ensures every node is copied exactly once and all connections are preserved correctly, even across cycles. Finally, I return the clone of the initial node stored in the map.

Common Mistakes to Avoid

  • Creating a shallow copy instead of a deep copy, resulting in shared references between original and cloned graphs
  • Failing to detect cycles, causing the algorithm to enter an infinite loop when traversing undirected edges
  • Using recursion without considering stack depth limits, which can crash on large graphs common in enterprise systems
  • Neglecting to initialize the map with the starting node before beginning the traversal loop

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 49 Salesforce questions