KatInterview

Split a Nested Structure into Values and a Shape Template

Trees & GraphsAuto-graded · 22 test cases
MediumAuto-gradedTrees & Graphsrecursiondfsnested-structures

Last updated

Implement a single function that extracts leaves and a shape template from nested lists and dicts, then rebuilds the original from them, testing recursion and traversal order.

Values and Shape Template

You are given an arbitrarily nested structure built from Python lists and dictionaries. Anything that is not a list or a dict (integers, floats, strings, booleans, None, ...) is a leaf.

Write a single function that does three things and returns all three results:

  1. Collect the leaves. Produce a flat list values of every leaf, in depth-first, left-to-right order. Lists are visited by index order. Dictionaries are visited in their key insertion order.
  2. Build a shape template. Produce a template that has the same shape as the input, but with each leaf replaced by the marker string "V". Lists keep their length, and dicts keep the same keys in the same order. Empty containers stay empty.
  3. Rebuild the input. Using only values and template, reconstruct a structure equal to the original input by placing the k-th leaf value into the position of the k-th "V" marker in the template.

The function returns a list of exactly three items: [values, template, rebuilt].

Rules

  • Only list and dict count as containers. Every other object is a leaf, including a string that happens to be "V". Such a string goes into values like any other leaf, and its slot in the template is still "V".
  • If the input itself is a leaf, then values is a one-element list holding it, the template is "V", and rebuilt is that same leaf.
  • An empty list or empty dict contributes no values and appears unchanged in the template.
  • Dictionary keys are strings.
  • The rebuilt structure must be equal to the input.

Constraints

  • The input contains only lists, dicts with string keys, and leaf values (ints, floats, strings, booleans, None).
  • The nesting depth is small enough for ordinary recursion to work.
  • The returned value is a list of three elements in the order values, template, rebuilt.

Examples

Example 1

Input: split_and_rebuild({'a': [1, 2], 'b': {'c': 3}})

Output: [[1, 2, 3], {'a': ['V', 'V'], 'b': {'c': 'V'}}, {'a': [1, 2], 'b': {'c': 3}}]

Example 2

Input: split_and_rebuild([1, {'x': [2, 3]}, 4])

Output: [[1, 2, 3, 4], ['V', {'x': ['V', 'V']}, 'V'], [1, {'x': [2, 3]}, 4]]

Example 3

Input: split_and_rebuild({'empty_list': [], 'empty_dict': {}, 'value': None})

Output: [[None], {'empty_list': [], 'empty_dict': {}, 'value': 'V'}, {'empty_list': [], 'empty_dict': {}, 'value': None}]

Example 4

Input: split_and_rebuild({'name': 'x', 'scores': [1.5, 2], 'active': True})

Output: [['x', 1.5, 2, True], {'name': 'V', 'scores': ['V', 'V'], 'active': 'V'}, {'name': 'x', 'scores': [1.5, 2], 'active': True}]

Hints

Hint 1

The template is the input with every leaf swapped for a marker. Build it with one recursive pass that appends each leaf to a shared list as it is encountered.

Hint 2

To rebuild, walk the template in the same order as before and keep a cursor into `values`. Each time you meet a marker, take the next value and advance the cursor.

Hint 3

Avoid `pop(0)` or slicing for consuming values, since these are O(n) each. Use an index counter or an iterator. Check for list and dict types before treating something as a leaf.