Infer a schema from JSON records

Python data engineering interview problem. Difficulty: advanced. Pattern: Schema Drift. About 18 minutes. Part of the Pro drill bank.

Infer data types and detect nullability or union conflicts across an iterable of JSON-like records. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement infer_schema(records: list[dict]) -> dict[str, str] to derive column data types from an iterable of JSON-like dictionaries. Inspect each record to identify types for all encountered fields: bool, int, float, str, list, dict. (Note: in Python booleans inherit from int, so check bool first). If a field is omitted in any record or explicitly set to None, mark the field as nullable by appending ? to its type. If a field contains multiple distinct non-null types across records (schema drift), sort the type names alphabetically, join with |, and append ? if nullable (for example, int|str?). If a field only ever contains None (or is missing across all records), report null?. Return the final dictionary with keys sorted alphabetically.

Requirements

  • Return keys in alphabetical order.
  • Distinguish bool from int.

Constraints

  • 0 <= len(records) <= 10,000

Examples

Input: infer_schema([{'a': 1, 'b': 'x'}, {'a': 2, 'b': None}]) Output: {'a': 'int', 'b': 'str?'} Field a is always int and non-null; field b contains None in 1 row so it is nullable.

Topics: lakebench, python, type-inference, union-types.

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

advanced

Infer a schema from JSON records

Interview-style drill: Infer data types and detect nullability or union conflicts across an iterable of JSON-like records.

Implement `infer_schema(records: list[dict]) -> dict[str, str]` to derive column data types from an iterable of JSON-like dictionaries. - Inspect each record to identify types for all encountered fields: `bool`, `int`, `float`, `str`, `list`, `dict`. (Note: in Python booleans inherit from int, so check bool first). - If a field is omitted in any record or explicitly set to `None`, mark the field as nullable by appending `?` to its type. - If a field contains multiple distinct non-null types across records (schema drift), sort the type names alphabetically, join with `|`, and append `?` if nullable (for example, `int|str?`). - If a field only ever contains `None` (or is missing across all records), report `null?`. - Return the final dictionary with keys sorted alphabetically.