Drill

ProblemsPython › validation

Which required fields are missing

mediumvalidationPython

A form submission arrives as a bag of strings and the server checks it before touching the database.

missing_fields(payload: map<string, string>, required: list<string>) → list<string>

Solve it in the editor →

Where you start

def missing_fields(payload: dict[str, str], required: list[str]) -> list[str]:
    

Worked examples

CallResult
missing_fields({"name": "Ada", "email": ""}, ["name", "email", "phone"])["email", "phone"]
missing_fields({"name": " "}, ["name"])["name"]
missing_fields({"name": "Ada"}, ["name"])[]
missing_fields({}, [])[]

Hint

Walk the requirements, not the payload — that is what fixes the output order.

Reference solution in Python
def missing_fields(payload: dict[str, str], required: list[str]) -> list[str]:
    return [k for k in required if not payload.get(k, '').strip()]

The same problem in another language

More validation problems in Python