EvalFrame Technical Mock Interviews & Coding Practice

Clone Graph

Medium · Graph · Algorithm problem · all problems

Given an adjacency list representing an undirected graph, return a deep copy (clone) of the graph representation. Each node in the graph contains a value (`val`) and a list of its neighbors. **Examples:** ```typescript cloneGraph([[2,4],[1,3],[2,4],[1,3]]) // [[2,4],[1,3],[2,4],[1,3]] cloneGraph([[]]) // [[]] cloneGraph([]) // [] ```

Solve It in the BrowserPractice Explaining It Out Loud

Optimal Approach

  • Time complexity: O(V + E)
  • Space complexity: O(V)

Traversing the graph requires visiting all vertices V and edges E.

Follow-Up Questions Interviewers Ask About Clone Graph

  • How do you handle cycles when cloning graph node objects in memory?
    Use a hash map mapping each original Node pointer to its newly cloned Node pointer to avoid infinite recursion.

Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.