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

Validate JSON records against a schema

Easy·Acceptance ·Asked 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"

Example

records = [{"id": 1, "name": "a"}, {"id": "x", "name": "b"}, {"id": 3}] schema = {"id": "int", "name": "str"} Output valid = [{"id": 1, "name": "a"}] rejects = [{"record": {"id": "x", "name": "b"}, "field": "id", "reason": "type"}, {"record": {"id": 3}, "field": "name", "reason": "missing"}]

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

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.