xAI

Verified interview challenge

Flatten and rebuild nested structures

Traverse nested containers in a precise order, then reconstruct their structure from a flat sequence.

Unlock with Full Access

The challenge

Lists, tuples and dictionaries can be mixed throughout the input. The first part collects their integer leaves, and the second rebuilds the same shape with new leaf values. Preserve container types, positions and ordering, including empty containers, while handling deep inputs without relying on Python's recursion limit.

What you'll practise

  • Traversal consistency

    Use the same encounter order in both directions without modifying inputs.

  • Shape preservation

    Retain the distinction between leaves, keys and empty container structure.

Your coding workspacePreview
Current partFurther parts unlock as you progress
solution.pyPython 3.13
# Build on your implementation

# Run checks for the current part

# Full starter code included
Current-part checks

Run your code to see results.

The complete sequence, starter code and checks are included with Full Access.