Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Deep Clone Complex Objects

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

Your question is Deep Clone Complex Objects. 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

Amazon Alexa components may store nested state using dictionaries, lists, and sets. Implement deep_clone to create a fully independent copy while preserving the original structure's shared references and supporting cycles.

Formal Specification

The function receives one Python value containing only:

  1. Immutable primitives: None, booleans, integers, floats, strings, and bytes.
  2. Mutable containers: dictionaries, lists, and sets.
  3. Nested combinations of these values.

Return a deep clone with these guarantees:

  • No mutable container in the clone is the same object as its counterpart in the input.
  • Repeated references remain repeated references in the clone. If two fields reference the same original list, they must reference the same cloned list.
  • Cyclic references must terminate and remain cyclic in the clone.
  • Dictionary keys and values must be cloned when they contain supported compound values.
  • The input must not be modified.

You may use id(value) to identify object identity. Inputs contain at most 10,000 container objects and a nesting depth of at most 1,000.

Constraints

  • Supported values are None, booleans, integers, floats, strings, bytes, dictionaries, lists, and sets.
  • There are at most 10,000 container objects.
  • Nesting depth is at most 1,000.
  • Dictionary keys and set members are hashable.
  • The input may contain aliases and cyclic references.

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