Clone a List with Index-Based Cross References
Arrays & HashingAuto-graded · 18 test casesLast updated
Tests your ability to perform a deep copy of a nested list structure while preserving integer-indexed references between elements.
Problem Statement
You receive a list where each element is a two-item array [value, reference_index]. The value is an integer, and reference_index is either an integer pointing to a position in the list or null.
Your task is to create a deep copy of this list. The copy must contain entirely new nested arrays, not references to the original arrays. However, the structure and all values must be identical.
Input Format
A list of arrays, where each array contains:
value: an integerreference_index: an integer index (0-based) pointing to another element in the list, ornull
Output Format
A new list with the same structure, where all nested arrays are newly created objects.
Constraints
0 <= n <= 1000wherenis the length of the list-10000 <= value <= 10000reference_indexis eithernullor a valid index0 <= reference_index < n
Edge Cases
- An empty list returns an empty list
- A single element with
reference_indexasnullor0(pointing to itself) - Multiple elements with the same value but different reference indices
Examples
Example 1
Input: clone_reference_list([[7, None], [13, 0], [11, 4], [10, 2], [1, 0]])
Output: [[7, None], [13, 0], [11, 4], [10, 2], [1, 0]]
Example 2
Input: clone_reference_list([])
Output: []
Example 3
Input: clone_reference_list([[1, None]])
Output: [[1, None]]
Example 4
Input: clone_reference_list([[5, 0], [5, 1], [5, None]])
Output: [[5, 0], [5, 1], [5, None]]
Hints
Hint 1
Think about what 'deep copy' means for nested structures—you need to create new inner arrays, not just copy references.
Hint 2
Python's list slicing creates a shallow copy of the outer list, but what about the inner arrays?
Hint 3
Consider using list comprehension to create new arrays element by element.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core challenge is understanding what constitutes a "deep copy" for this structure. While the outer list and inner arrays must be newly created, the primitive values (integers and null) are immutable and can be copied directly.
The solution uses a list comprehension to iterate through each element in the input. For each [value, reference_index] pair, we construct a brand new array containing the same two values. This ensures that modifying the inner arrays in the result won't affect the original input.
A common mistake is to use simple list slicing like nodes[:], which creates a shallow copy of the outer list but keeps references to the same inner arrays. Another approach that fails is list(nodes), which has the same shallow-copy problem.
Why It Works
- The list comprehension
[[value, reference_index] for value, reference_index in nodes]creates a new outer list - Each iteration constructs a new inner array
[value, reference_index]rather than reusing the original - The unpacking
for value, reference_index in nodesextracts the primitive values, and those primitives are placed into fresh arrays - Since integers and
Noneare immutable in Python, copying their values is sufficient—there's no deeper level of structure to clone
Complexity
Time Complexity: O(n) where n is the number of elements. We visit each element once and perform constant work (creating a new two-element array).
Space Complexity: O(n) for the output list. The solution uses minimal auxiliary space beyond the required output.
Implementation
from typing import List, Optional
def clone_reference_list(nodes: List[List[Optional[int]]]) -> List[List[Optional[int]]]:
"""
Create a deep copy of a list where each element contains a value and an optional reference index.
Args:
nodes: List of [value, reference_index] pairs
Returns:
A new list with the same structure but entirely new nested arrays
"""
copies = [[value, reference_index] for value, reference_index in nodes]
return copies