Check a structured reply

Check a structured reply

Databricks LLM Interview Question

A model told to return JSON will sometimes return the right shape with the wrong types, such as an age as the string "36". The code has to find that before it reaches the database.

Write a function invalid_fields(reply, schema) that returns the names of the fields that are missing from the reply or hold the wrong type, sorted alphabetically. The schema maps a field name to one of "string", "number" or "boolean". Fields in the reply that are not in the schema are ignored. In JSON a number may be whole or decimal, and true is a boolean rather than a number.

Asked of

  • AI Engineer
  • ML Engineer
  • Data Scientist

Example 1

Input

reply = {"name": "Ada", "age": "36"}, schema = {"name": "string", "age": "number", "active": "boolean"}

Output

["active", "age"]

Example 2

Input

reply = {"score": 9.5, "passed": True}, schema = {"score": "number", "passed": "boolean"}

Output

[]

Explanation

In the first example the age is a string where the schema asks for a number, and the active field is missing altogether. The name is correct, so it is not listed.

Submit also runs 3 hidden test cases that check edge cases.

Similar questions