Copy Random List

Python data engineering interview problem. Difficulty: intermediate. Pattern: Linked Lists. About 14 minutes. Part of the Pro drill bank.

Deep-copy a list where each node has a random index pointer. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Some linked-list nodes have a second pointer that can jump to any node. Copying only the values is not enough because the jumps must point into the new copy. Return a deep copy of the node records. Each record is [value, random_index]; preserve every value and random index, but make the result independent of the input records. Write copy_random_list(nodes: list) -> list. [[7, None], [13, 0], [11, 4], [10, 2], [1, 0]] keeps the same value and random-index shape in the copy. The starter file already has the function name and harness print lines. Keep them and fill only the function body.

Requirements

  • Copy every node value.
  • Preserve None and every random target index.
  • Do not reuse the original nested records.

Constraints

  • A random index is an index into the same node list or None.
  • Return a list of 2-item records with the same length.

Examples

Input: copy_random_list([[7, None], [13, 0], [11, 4], [10, 2], [1, 0]]) Output: [[7, None], [13, 0], [11, 4], [10, 2], [1, 0]] Structure preserved.

Topics: lakebench, python, deep copy, random.

More Python interview questions · All interview problems · Learn data engineering

intermediate

Copy Random List

Interview-style drill: Deep-copy a list where each node has a random index pointer.

Implement `copy_random_list(nodes: list) -> list`. Input nodes are `[[val, random_index], ...]` where `random_index` is an index into nodes or `None`. Return the same shape for a deep copy (values and random indices unchanged). Example: `[[7, None], [13, 0], [11, 4], [10, 2], [1, 0]]` → same structure. Keep the harness.