KatInterview

Flatten and Filter a Nested Configuration Tree

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designdata structuresrecursioniterators

Last updated

Tests the ability to write an iterator-style traversal over arbitrarily nested dicts and lists to extract leaf values matching a type filter.

Flatten and Filter a Nested Configuration Tree

Modern applications store configuration as deeply nested structures — a mix of dictionaries (keyed sections) and lists (ordered collections) that can be nested inside one another to arbitrary depth. You need to extract all leaf values from such a structure and return them in a specific order, optionally filtered by type.

Definitions

  • A leaf value is any value that is not a dict and not a list.
  • Traversal order follows depth-first, left-to-right rules:
    • For a dict, process keys in lexicographic (alphabetical) order.
    • For a list, process elements in index order (0, 1, 2, …).
  • The type_filter argument is a string that may be:
    • "any" — return all leaf values regardless of type.
    • "int" — return only leaves where isinstance(value, int) and not isinstance(value, bool).
    • "float" — return only leaves where isinstance(value, float).
    • "str" — return only leaves where isinstance(value, str).
    • "bool" — return only leaves where isinstance(value, bool).
    • "none" — return only leaves that are None.

Task

Implement:

def collect_leaves(config: dict, type_filter: str) -> list:
  • config is the root, which is always a dict (possibly empty).
  • Returns a list of leaf values (in traversal order) that match type_filter.

Notes

  • bool is a subclass of int in Python, so you must distinguish them carefully:
    • True and False match "bool" but not "int".
    • Integer values like 0, 1, 42 match "int" but not "bool".
  • float values like 3.14 do not match "int", even though they might be whole numbers.
  • The root is always a dict; nested structures may contain dicts or lists at any level.
  • An empty dict or empty list contributes no leaves.

Constraints

  • config is always a dict at the root level.
  • Nesting depth ≤ 20.
  • Total number of nodes (dicts + lists + leaves) ≤ 2,000.
  • type_filter is always one of: "any", "int", "float", "str", "bool", "none".
  • Leaf values are drawn from: int (non-bool), float, str, bool, None.
  • Dict keys are always strings.

Examples

Example 1

Input: collect_leaves({'z': 1, 'a': [True, 'hello', 2], 'm': {'x': None, 'b': 3.14}}, 'int')

Output: [2, 1]

Example 2

Input: collect_leaves({'server': {'host': 'localhost', 'port': 8080, 'debug': True}, 'limits': [100, 200, 300]}, 'any')

Output: [100, 200, 300, True, 'localhost', 8080]

Example 3

Input: collect_leaves({'alpha': {'beta': {'gamma': False}}, 'delta': []}, 'bool')

Output: [False]

Hints

Hint 1

Use a recursive helper (or an explicit stack) that processes dicts by sorted keys and lists by index; any non-dict, non-list value is a leaf to consider.

Hint 2

Be careful with Python's type hierarchy: isinstance(True, int) is True, so you need an explicit bool check before the int check when filtering.

Hint 3

An iterative approach with a stack works well — push dict values in reverse-sorted-key order (or list elements in reverse index order) so that popping gives you the correct left-to-right traversal.