Validate Key-Value Records Against a Schema
Problem statement
Validate key-value records against a small schema.
Each schema rule has the form:
key:type:requiredtypeisSTRING,INT, orBOOL.requiredistrueorfalse.- Schema keys are unique.
Each record is a semicolon-separated list of key=value fields. Return one boolean per record.
A record is valid only when:
- every field has a nonempty key and contains
=; - every key belongs to the schema and appears at most once;
- every required key appears;
- a
STRINGvalue is nonempty; - an
INTvalue is an optional leading minus sign followed by one or more decimal digits; and - a
BOOLvalue is exactlytrueorfalse.
What the interview report shared
During the reported internship team screen, the author completed three parts of a key-value data-validation task. Its schema, validation rules, and output contract were not preserved.
Function
validateKeyValueRecords(schema: String[], records: String[]) → boolean[]Examples
Example 1
schema = ["id:STRING:true","age:INT:true","active:BOOL:false"]records = ["id=u1;age=20;active=true","id=u2;age=twenty","age=7;id=u3"]return = [true,false,true]The first record satisfies every rule. The second has an invalid integer. The third contains both required keys, and field order does not matter.
Example 2
schema = ["name:STRING:true","score:INT:false"]records = ["name=Ada;score=-3","name=","name=Lin;name=Kai","name=Bo;extra=x"]return = [true,false,false,false]The invalid records contain an empty required string, a repeated key, and an unknown key.
Constraints
1 <= schema.length <= 100.1 <= records.length <= 10^5.- Keys contain lowercase English letters, digits, or underscores.
- Each record has at most
100fields and at most1000characters. - Values contain printable ASCII characters other than
;.