KatInterview

Parse and Validate Business Registration CSV Records

StringsAuto-graded · 21 test cases
MediumAuto-gradedStringsstringsparsingvalidation

Last updated

Tests multi-rule sequential validation over CSV rows: field presence, tax ID format normalization, and fuzzy owner name matching via token overlap.

Parse and Validate Business Registration CSV Records

A business registry system receives CSV exports of company onboarding submissions. Each row contains five fields describing a business and its ownership. Your job is to parse the CSV and run a series of checks against each record, returning a structured validation report.

Write a function validate_business_records(csv_data: str) -> list[dict] that takes the raw CSV text and returns one result entry per data row.

Input Format

csv_data is a multi-line string. The first line is always the header:

record_id,business_name,tax_id,legal_owner_name,submitted_owner_name

Any field may have leading or trailing whitespace; strip it before processing. Fields do not contain commas or quoted values.

Validation Rules

Apply the following checks to each row, in this order:

Rule 1 — Required Fields All five fields must be non-empty after stripping whitespace. If one or more are empty, add "MISSING_REQUIRED_FIELD" to the error list exactly once, then continue checking the remaining rules against whichever fields are present.

Rule 2 — Tax ID Format Only run this check when tax_id is non-empty. Remove all hyphens (-) and spaces from tax_id, then verify that the result is exactly 9 characters long and every character is an ASCII digit (0–9). Any other character, including Unicode digit-like symbols, is invalid. If the check fails, add "INVALID_TAX_ID".

Rule 3 — Owner Name Match Only run this check when both legal_owner_name and submitted_owner_name are non-empty. Normalize each name by:

  1. Converting every character to lowercase.
  2. Replacing every character that is not a letter, digit, or space with a space.
  3. Splitting on whitespace to produce a set of unique tokens.

Let S be the token set of submitted_owner_name and L be the token set of legal_owner_name.

The names are considered a match when both of the following hold:

  • At least one token is shared: |S ∩ L| ≥ 1
  • The shared tokens cover at least half of the submitted tokens: |S ∩ L| / |S| ≥ 0.5

If either condition fails, or if S is empty (e.g., the name consists entirely of symbols), add "OWNER_NAME_MISMATCH".

A record can accumulate errors from multiple rules. Errors always appear in the order listed above (Rule 1 → Rule 2 → Rule 3).

Output Format

Return a list of dictionaries, one per data row, in the same order they appear in the input:

[
    {
        "record_id": str,    # trimmed value, or "" if the field was absent
        "is_valid": bool,    # True only when errors is empty
        "errors": list[str]  # error codes in rule order
    },
    ...
]

Constraints

  • The input contains between 1 and 10,001 lines (1 header + up to 10,000 data rows).
  • Fields do not contain commas or escaped quotes.
  • A completely blank line in the CSV body is treated as a row of empty fields and will produce a "MISSING_REQUIRED_FIELD" error with record_id: "".
  • Extra columns beyond the fifth are ignored; missing trailing columns are treated as empty strings.

Examples

Example 1

Input: validate_business_records('record_id,business_name,tax_id,legal_owner_name,submitted_owner_name\nrec_1,Acme Corp,12-3456789,Jane Doe,Jane Doe\nrec_2,Beta LLC,99-999,Robert Smith,Bob Smith\nrec_3,Gamma Inc,11-2233445,Alice M. Walker,Alice Walker\n')

Output: [{'record_id': 'rec_1', 'is_valid': True, 'errors': []}, {'record_id': 'rec_2', 'is_valid': False, 'errors': ['INVALID_TAX_ID']}, {'record_id': 'rec_3', 'is_valid': True, 'errors': []}]

Example 2

Input: validate_business_records('record_id,business_name,tax_id,legal_owner_name,submitted_owner_name\n r1 , Acme Corp , 123 456 789 , Jane Doe , Jane Doe \nr2,Beta LLC,12345678A,Robert Smith,Robert Jones\n')

Output: [{'record_id': 'r1', 'is_valid': True, 'errors': []}, {'record_id': 'r2', 'is_valid': False, 'errors': ['INVALID_TAX_ID']}]

Example 3

Input: validate_business_records('record_id,business_name,tax_id,legal_owner_name,submitted_owner_name\n')

Output: []

Hints

Hint 1

Start by using Python's csv.reader (wrapped around io.StringIO) to parse the rows — it handles edge cases like blank lines and short rows more cleanly than splitting on commas manually. Pad each parsed row to exactly five fields with empty strings.

Hint 2

For the tax ID check, strip hyphens and spaces first, then verify length and character set. Use a character-by-character comparison against '0'–'9' rather than str.isdigit(), which accepts non-ASCII digit characters.

Hint 3

For the name match, replace non-alphanumeric characters with spaces (not empty string) before splitting — this ensures punctuation like the period in 'Alice M.' or the apostrophe in 'O'Brien' correctly separates tokens. Then measure the overlap ratio against the size of the *submitted* token set.