Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Deep Clone and Pitfalls
00:00
5 left

Deep Clone and Pitfalls

HardPython

Problem

The Twitch Creator Dashboard may need an independent copy of nested editor state before applying speculative updates. Implement deep_clone(obj) to clone a supported object graph without sharing mutable containers with the original.

The input may contain nested dictionaries and lists. Dictionary keys are strings, and leaf values are JSON-compatible primitives: strings, numbers, booleans, or None. The input can also contain repeated references or cycles, even though JSON examples cannot represent object identity directly.

Requirements

  1. Return a structurally equivalent clone.
  2. Mutating any cloned dictionary or list must not mutate the original object.
  3. Preserve graph topology: if two original fields reference the same mutable object, the corresponding cloned fields must reference the same cloned object.
  4. Handle cycles without infinite recursion.
  5. Raise TypeError for unsupported value types.

Constraints

  • The object graph contains at most 10^4 dictionaries and lists
  • Dictionary keys are strings
  • Supported leaves are strings, integers, floats, booleans, and None
  • The maximum nesting depth is at most 500
  • Cycles and repeated references may occur

Function Signature

def deep_clone(obj):
Interviewer

Your question is Deep Clone and Pitfalls. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.