Drill

ProblemsC# › validation

Which required fields are missing

mediumvalidationHash mapsArraysC#

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>

C# 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

public List<string> MissingFields(Dictionary<string, string> payload, List<string> required) {
    
}

Worked examples

CallResult
MissingFields(new Dictionary<string, string> { { "name", "Ada" }, { "email", "" } }, new List<string> { "name", "email", "phone" })new List<string> { "email", "phone" }
MissingFields(new Dictionary<string, string> { { "name", " " } }, new List<string> { "name" })new List<string> { "name" }
MissingFields(new Dictionary<string, string> { { "name", "Ada" } }, new List<string> { "name" })new List<string> { }
MissingFields(new Dictionary<string, string> { }, new List<string> { })new List<string> { }

Hint

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

Reference solution in C#
public List<string> MissingFields(Dictionary<string, string> payload, List<string> required) {
    var result = new List<string>();
    foreach (var key in required) {
        if (!payload.ContainsKey(key) || payload[key].Trim().Length == 0) result.Add(key);
    }
    return result;
}

The same problem in another language

More validation problems in C#