Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Clone Graph

Python data engineering interview problem. Difficulty: advanced. Pattern: Graphs. About 18 minutes. Part of the Pro drill bank.

Deep-copy an undirected graph given as an adjacency dict. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement clone_graph(adj: dict) -> dict. Keys are node labels; values are neighbor lists. Return a deep copy where each neighbor list is sorted ascending.

Constraints

  • Neighbor lists in the result must be sorted.

Examples

Input: clone_graph({1: [2, 4], 2: [1, 3], 3: [2, 4], 4: [1, 3]}) Output: {1: [2, 4], 2: [1, 3], 3: [2, 4], 4: [1, 3]} Deep copy preserves edges.

Topics: lakebench, python, clone, dfs.

More Python interview questions · All interview problems · Learn data engineering

advanced

Clone Graph

Interview-style drill: Deep-copy an undirected graph given as an adjacency dict.

Implement `clone_graph(adj: dict) -> dict`. Keys are node labels; values are neighbor lists. Return a deep copy where each neighbor list is sorted ascending. Example: `{1: [2, 4], 2: [1, 3], 3: [2, 4], 4: [1, 3]}` → same with sorted neighbors. Keep the harness.