Deep-Copy an Object Graph

Hard ⏱ 18 min 31% acceptance ★★★★★ 4.9
Team(name, members) holds a list. Write clone_team(team) using copy.deepcopy so mutating the clone's members list never touches the original, and shallow_clone_team(team) using copy.copy to demonstrate the shared-list pitfall. Return both from the respective functions.

Examples

Example 1
Input
t = Team('core', ['ana']); c = clone_team(t); c.members.append('raj'); len(t.members)
Output
1
Explanation

Deep copy isolated the nested list.

Constraints

  • Use the copy module, not manual reconstruction.
  • Both functions required.

Topics

OOPcopying objects

Companies

AtlassianAsanaMonday.com

Hints

Hint 1

copy.copy duplicates the object but shares nested mutables.

Hint 2

copy.deepcopy recurses.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.