Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Deep Clone Without Libraries

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Deep Clone Without Libraries. Start with the requirements on the right.

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.

Problem

Netcracker Digital Platform configuration data can contain deeply nested dictionaries and lists. Implement a function that creates a fully independent clone without using external libraries.

The input is a Python value composed of dict, list, str, int, float, bool, and None. Dictionary keys are strings. Mutable objects may be shared by multiple fields or may reference themselves indirectly.

Your clone must satisfy these requirements:

  1. Copy every dictionary and list so that modifying the clone cannot modify the original.
  2. Return immutable values directly because they do not require cloning.
  3. Preserve internal aliases. If two fields in the input reference the same mutable object, the corresponding fields in the clone must reference the same cloned object.
  4. Preserve cycles, such as a list that contains itself.
  5. Do not use copy.deepcopy or any external library.

The function should return the cloned value.

Constraints

  • The input contains only dict, list, str, int, float, bool, and None values.
  • Dictionary keys are strings.
  • The total number of dictionaries and lists is at most 10^5.
  • The input may contain shared references or cycles.
  • The nesting depth may be large.

Function Signature

def deep_clone(value):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output