Problem · Array

Validate Typed CSV Records

Learn this problem
MediumStripe logoStripeFULLTIMEPHONE SCREEN
See Stripe hiring insights

Problem statement

Validate CSV records that have already been split into fields. columnTypes describes the columns in order, and required[i] says whether column i must be nonempty.

Each nonempty field must follow its declared type:

  • STRING: any printable ASCII string.
  • INTEGER: an optional leading minus sign followed by one or more digits.
  • DECIMAL: an optional leading minus sign, one or more digits, and an optional decimal point followed by one or more digits. Exponents are not accepted.
  • BOOLEAN: exactly true or false.

A record is valid exactly when it has the same number of fields as columnTypes, every required field is nonempty, and every nonempty field matches its type. An empty optional field is valid without further type checking. Return one boolean per record in input order.

Function

validateCsvRecords(columnTypes: String[], required: boolean[], records: String[][]) → boolean[]

Examples

Example 1

columnTypes = ["INTEGER","STRING","BOOLEAN"]required = [true,true,false]records = [["42","alice","true"],["-7","","false"],["1","bob",""],["3.5","eve","true"]]return = [true,false,true,false]

The second record omits a required string. The third record may leave its optional boolean empty. The fourth record uses a decimal where an integer is required.

Example 2

columnTypes = ["DECIMAL","INTEGER"]required = [true,false]records = [["12.50","7"],["-3",""],[".5","2"],["1e3","4"],["5","2.0"]]return = [true,true,false,false,false]

The first two records follow the grammar. A decimal must have a digit before the decimal point, exponent notation is unsupported, and 2.0 is not an integer.

Constraints

  • 1 <= columnTypes.length == required.length <= 100.
  • 1 <= records.length <= 100000.
  • Every column type is STRING, INTEGER, DECIMAL, or BOOLEAN.
  • Every field contains at most 100 printable ASCII characters.
  • The total number of fields across all records is at most 200000.
  • Input fields are already decoded; CSV quoting and delimiter parsing are outside this exercise.

More Stripe problems

drafts saved locally
public boolean[] validateCsvRecords(String[] columnTypes, boolean[] required, String[][] records) {
    // Write your code here.
}
columnTypes["INTEGER","STRING","BOOLEAN"]
required[true,true,false]
records[["42","alice","true"],["-7","","false"],["1","bob",""],["3.5","eve","true"]]
expected[true,false,true,false]
checking account