FastPrepValidate Key-Value Records Against a Schema

Validate Key-Value Records Against a Schema

Stripe logoStripe● MediumINTERNPHONE SCREEN
Learn

Problem statement

Validate key-value records against a small schema.

Each schema rule has the form:

key:type:required
  • type is STRING, INT, or BOOL.
  • required is true or false.
  • 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 STRING value is nonempty;
  • an INT value is an optional leading minus sign followed by one or more decimal digits; and
  • a BOOL value is exactly true or false.

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 100 fields and at most 1000 characters.
  • Values contain printable ASCII characters other than ;.

More Stripe problems

See Stripe hiring insights
public boolean[] validateKeyValueRecords(String[] schema, String[] records) {
    // write your code here
}
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"]
expected[true,false,true]
Checking account…