← Problems21. Validate JSON records against a schemaEasyPython
00:00 / 20:00

Validate JSON records against a schema

Easy100% solve itAsked at Microsoft, Deloitte

Split a batch of ingested records into valid rows and rejects. A record is valid when every required field is present, non-null, and of the declared type.

Input schema

solve(records: list[dict], schema: dict[str, str]) -> tuple[list[dict], list[dict]] schema values are one of: "int" | "float" | "str" | "bool"

Constraints

  • Report the first failing field in schema order, not all of them
  • bool is not an acceptable int
  • Input order is preserved in both lists
  • Up to 500,000 records

Example 1one valid row, one bad type, one missing field

Input · schema
{"id": "int", "name": "str"}
Input · records
idname
1a
xb
3null
Output
[
  [{"id": 1, "name": "a"}],
  [{"field": "id", "reason": "type", "record": {"id": "x", "name": "b"}}, {"field": "name", "reason": "missing", "record": {"id": 3}}]
]

Example 2a null value counts as missing

Input · schema
{"id": "int", "name": "str"}
Input · records
idname
nulla
Output
[
  [],
  [{"field": "id", "reason": "missing", "record": {"id": null, "name": "a"}}]
]

Topics

data modeling

Similar problems

Community-reported interview topic. Not an official company question and no affiliation is implied.

solution.py
Loading editor…
Draft not saved yet · Spaces 4 · UTF-8⌘↵ run ⌘⇧↵ submit
Nothing run yet

Run against the public tests, or submit to score against all of them.