Home › Company-wise questions › Capgemini › Clone Graph ← Capgemini bank
Capgemini · Question 59 · Graphs
Clone Graph Graphs C++ code ⏱ O(n+e) 💾 O(n)
Service-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem You are given reference of node in connected undirected graph. Return deep copy.
Approach DFS-clone each node once, memoizing copies in a hashmap.
Time O(n+e)Space O(n)
How it works 1 Clone nodes on first DFS visit. 2 Memoize to preserve sharing and cycles; driver sorts for stability. Sample input / output Input:
adjList = [[2, 4], [1, 3], [2, 4], [1, 3]]
Output:
[[1, [2, 4]], [2, [1, 3]], [3, [2, 4]], [4, [1, 3]]] Constraints 0 <= nodes <= 100; connected undirected graph.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n+e)Space O(n)Loops 7Branches 3Returns 1source · C++
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
↻ repeat ↻ repeat ↻ repeat ↻ repeat ↻ repeat START using namespace std CLASS Node DEFINE val vector<Node*> neighbors Node(int v) : val(v) {} Node* cloneGraph(Node* node, unordered_map<Node*, Node*>& visited) Node* cloneGraph(Node* node) DEFINE main() vector<Node*> nodes for (int i = 1; i <= 4; ++i) nodes.push_back(ne… vector<vector<int>> links = {{2, 4}, {1, 3}, {2, 4}, {1, 3}} for (size_t i = 0; i < nodes.size(); ++i) for (int v : links[i]) nodes[i]->neighbors.push_back(nodes[v - 1]) Node* clone = cloneGraph(nodes[0]) set<Node*> seen vector<Node*> stack = {clone} vector<pair<int, vector<int>>> out while (!stack.empty()) Node* cur = stack.back() stack.pop_back() if (seen.count(cur)) continue seen.insert(cur) vector<int> adj for (Node* nb : cur->neighbors) adj.push_back(nb->val) stack.push_back(nb) sort(adj.begin(), adj.end()) out.push_back({cur->val, adj}) sort(out.begin(), out.end()) cout << "[" for (size_t i = 0; i < out.size(); ++i) if (i) cout << ", " cout << "[" << out[i].first << ", [" for (size_t j = 0; j < out[i].second.size(); ++j) if (j) cout << ", " cout << out[i].second[j] cout << "]]" cout << "]" << endl RETURN 0 END start / end statement condition loop returns definition← Q58 · Number of Islands Q60 · Course Schedule (Cycle Detection) → Solution code Core logic highlighted in green
Language Python Java C++source C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →