Drill

ProblemsJava › validation

Which required fields are missing

mediumvalidationHash mapsArraysJava

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

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

Java needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

List<String> missingFields(Map<String, String> payload, List<String> required) {
    
}

Worked examples

CallResult
missingFields(Main.<String, String>mp("name", "Ada", "email", ""), Main.<String>ls("name", "email", "phone"))Main.<String>ls("email", "phone")
missingFields(Main.<String, String>mp("name", " "), Main.<String>ls("name"))Main.<String>ls("name")
missingFields(Main.<String, String>mp("name", "Ada"), Main.<String>ls("name"))Main.<String>ls()
missingFields(Main.<String, String>mp(), Main.<String>ls())Main.<String>ls()

Hint

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

Reference solution in Java
List<String> missingFields(Map<String, String> payload, List<String> required) {
    List<String> result = new ArrayList<>();
    for (String key : required) {
        String v = payload.get(key);
        if (v == null || v.trim().isEmpty()) result.add(key);
    }
    return result;
}

The same problem in another language

More validation problems in Java