Schema Checker
An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L3
Problem
Given a record dict and a list of required keys, return a sorted list of keys that appear in required but NOT in record.
The schema says one thing. The data says another.
Example 1
Input:
record = {"age":30,"name":"Alice","email":"a@b.com"}, required = ["name","age","email","id"]Output:
["id"]
Example 2
Input:
record = {"a":1,"b":2}, required = ["a","b"]Output:
[]
Worked solution and explanation
Why this problem exists in real interviews
Schema validation shows up at every API boundary: request bodies, ETL records, config loaders. Interviewers use this to test whether you reach for set algebra, whether you respect the input contract (sorted output), and whether you handle the empty-required and empty-record edge cases without a tangle of branches.
Break down the requirements
Step 1: Frame as set difference
'Keys in required but not in record' is set(required) - set(record.keys()). Set difference is O(len(required)) average and reads exactly like the spec sentence, which is the strongest signal of correctness you can give an interviewer.
Step 2: Sort the result
Sets are unordered, but the spec wants a sorted list. Wrap the difference in sorted(...) to lock in deterministic output, which also makes the function trivially testable with assertEqual.
Step 3: Handle empty inputs naturally
Empty required returns []. Empty record returns sorted(required). The set-difference idiom handles both without a special case, so do not introduce one; extra branches are negative signal here.
The solution
def missing_keys(record: dict, required: list) -> list:
return sorted(set(required) - set(record.keys()))Common follow-up questions
- What changes if required keys can be nested (dotted paths like 'user.email')? (discuss splitting on '.', recursing into sub-dicts, or using jsonpath / pydantic for production cases.)
- How would you also report keys in record that are NOT in required (extras)? (second set difference in the other direction; return a tuple or dict with both lists.)
- How would you scale this to validate millions of records against the same schema? (compute set(required) once outside the hot loop, use frozenset, or compile a pydantic model for cached validation.)