Problem · Hash Table

Dataset Schema Validator

Learn this problem
MediumPinterest logoPinterestFULLTIMEONSITE INTERVIEW

Problem statement

Implement a validator for a dataset and its schema.

The schema is an array of unique entries in the form key:type. The only supported schema types are str and int.

Each record is an array of entries in the form key:type:value. The first two colons delimit the key and runtime type. Every remaining character belongs to the value, so a string value may itself contain colons.

Return true if every record contains every schema key exactly once, contains no extra keys, and gives every key the required runtime type. An int value must be a canonical signed decimal integer: 0, a positive number without leading zeros, or a negative number whose magnitude has no leading zeros. Otherwise, return false.

The entries inside a record may appear in any order. An empty dataset is valid.

Function

validateDataset(schema: String[], records: String[][]) → boolean

Examples

Example 1

schema = ["name:str","age:int","group:str"]records = [["name:str:Ada","age:int:31","group:str:search"],["group:str:ads","age:int:-4","name:str:Lin"]]return = true

Both records contain exactly the three schema keys, and every runtime type and integer representation is valid.

Example 2

schema = ["name:str","age:int"]records = [["name:str:Ada","age:str:31"]]return = false

The value for age carries the runtime type str, but the schema requires int.

Example 3

schema = ["message:str"]records = [["message:str:ready:now"],[]]return = false

The colon in the first string value is allowed, but the second record is missing the required key.

Constraints

  • 1 <= schema.length <= 50
  • 0 <= records.length <= 200
  • 0 <= records[i].length <= 50
  • Schema keys are unique and each schema entry is well formed with type str or int.
  • Keys and encoded values contain at most 100 characters.

More Pinterest problems

drafts saved locally
public boolean validateDataset(String[] schema, String[][] records) {
    // Write your solution here
}
schema["name:str","age:int","group:str"]
records[["name:str:Ada","age:int:31","group:str:search"],["group:str:ads","age:int:-4","name:str:Lin"]]
expectedtrue
checking account