KatInterview

Flat Error Collection from a Recursive Schema Validator

Trees & GraphsAuto-graded · 13 test cases
MediumAuto-gradedTrees & Graphsrecursionschema-validationflattening

Last updated

Tests ability to traverse a nested schema definition, flatten heterogeneous check results, and enforce a consistent empty-list contract across all node types.

Flat Error Collection from a Recursive Schema Validator

A schema-validation system checks records against a tree-shaped schema and produces a flat list of human-readable error strings for each record. Your job is to implement the engine that walks the schema tree, runs every check, and collects the results in document order.


Schema node types

A schema is a plain dictionary. Three node types exist, identified by the "type" key.

"field" — validates a single scalar value.

KeyTypeMeaning
"name"strfield name, used as the prefix in error messages
"required"bool (optional, default False)if True and the value is None, add "<name>: required"
"checks"list (optional, default [])ordered list of check descriptors to run when a value is present

If the value is None and the field is not required, contribute nothing (empty list).

"group" — validates a nested dict.

KeyTypeMeaning
"name"strgroup name
"required"bool (optional, default False)same rule as for "field"
"children"listordered list of child schema nodes

If the value is None and the group is not required, contribute nothing. If the value is present but is not a dict, add "<name>: expected object" and stop (do not recurse into children). Otherwise recurse into every child, collecting errors in order.

"each" — validates a list of items, each against the same sub-schema.

KeyTypeMeaning
"name"strnode name
"item"dictschema node applied to each element

If the value is None, contribute nothing. Otherwise run the item schema against every element in order and collect all errors.


Check descriptors

Each check descriptor is a dict with a "type" key. A check runs only when the field value is present (not None). The checks and the error strings they produce are:

"type"Extra keyError produced (or nothing if the rule passes)
"min_len""n": int"<name>: shorter than <n>"
"max_len""n": int"<name>: longer than <n>"
"digits_only"—"<name>: digits only"
"no_spaces"—one entry "<name>: space at <i>" per space character, in index order

A passing check contributes nothing. A check may contribute zero, one, or many error strings; all of them must appear in the output in the order they were produced.


Function signature

def validate_records(schema: dict, records: list[dict]) -> list[list[str]]:

Given a schema node and a list of records, return one flat list[str] per record containing every error message in document order (depth-first, children in the order they appear in the schema). If a record has no errors, return [] for that record, never None.


Constraints

  • 1 ≤ len(records) ≤ 1 000
  • Schema depth will not exceed 10 levels.
  • Field values, when present, are strings.
  • Record values for "group" nodes, when present, are either a dict or a non-dict (to trigger the "expected object" error).
  • Record values for "each" nodes, when present, are lists.
  • n in min_len / max_len checks is a non-negative integer.
  • An empty string is a valid present value and must be checked normally.
  • Every node type must return [] (not None) when it has nothing to report.

Examples

Example 1

Input: validate_records({'type': 'group', 'name': 'signup', 'children': [{'type': 'field', 'name': 'username', 'required': True, 'checks': [{'type': 'min_len', 'n': 3}, {'type': 'no_spaces'}]}, {'type': 'group', 'name': 'address', 'children': [{'type': 'field', 'name': 'zip', 'checks': [{'type': 'digits_only'}]}, {'type': 'field', 'name': 'city', 'checks': [{'type': 'min_len', 'n': 2}]}]}, {'type': 'each', 'name': 'tags', 'item': {'type': 'field', 'name': 'tag', 'checks': [{'type': 'max_len', 'n': 8}]}}]}, [{'username': 'jo e', 'address': {'zip': '9a1', 'city': 'Rome'}, 'tags': ['ok', 'verylongtag']}, {'username': 'maria', 'tags': []}, {'username': 'maria', 'address': {'zip': '', 'city': ''}}, {'address': 'Main St'}])

Output: [['username: space at 2', 'zip: digits only', 'tag: longer than 8'], [], ['zip: digits only', 'city: shorter than 2'], ['username: required', 'address: expected object']]

Hints

Hint 1

Start with the 'field' node: iterate its checks, and think carefully about what to do with the result of each one — a check can return None, a single string, or a list of strings. You need all three cases handled without leaking None into your output list.

Hint 2

For 'group' and 'each' nodes, the key question is how to combine child results into the parent's running list. append vs extend makes all the difference between a nested structure and a flat one.

Hint 3

Pick one canonical return value for 'nothing to report' and use it everywhere — field, group, and each — so callers never need to branch on None vs [].