KatInterview

Config-Driven CSV Validator with Name Overlap Check

StringsModel answer
MediumModel answerStringscsv-parsingvalidationregex

Last updated

Write a configurable validator that flags missing fields, length violations, banned words, and low name-word overlap across CSV vendor rows.

A vendor-onboarding pipeline receives a CSV export: a header line specifies the column names, and every subsequent line describes one vendor. You need to build a validator that inspects each record against a rule set provided as configuration and reports which rules that record violates. No rule details (column names, required columns, length bounds, banned vocabulary, the columns to compare for naming consistency, how strict that comparison is) may be baked into the code — all of it arrives via the config, and you should describe the config shape you assume.

vendor_id,vendor_name,legal_name,notes
1,Acme Robotics,Acme Robotics LLC,Primary supplier
2,,Bright Labs LLC,Missing display name
3,  ACME   Robotics ,acme robotics llc,Spacing and case only
4,Zed,Zed Logistics Inc,Display name too short

Function Signature

def validate_vendor_records(csv_text: str, config: dict) -> list[dict]:

Rules

  • The first line is the header; each following line is one record. A value can contain commas when quoted, so use a proper CSV parser — never split(",").
  • The validator is read-only with respect to records: it reports problems and never rewrites, sorts, removes, or merges rows.
  • config provides: required_fields, length_limits (per-field (min, max) measured after trimming), forbidden_words (whole-word, case-insensitive, checked on every field), overlap_fields (a pair of columns to compare), ignored_words (removed before comparison, e.g. llc, inc), and min_overlap_ratio.
  • A field is missing when, after trimming, it is empty or whitespace only.
  • Overlap check: for each of the two configured columns, lowercase the value, strip punctuation, tokenize on whitespace, remove ignored_words, then compute the Jaccard ratio |intersection| / |union| between the two token sets. If both columns are non-empty and the ratio falls below min_overlap_ratio, emit "name_mismatch".
  • Every record appears in the output, in input order, even when its issue list is empty.

Part 1 — Per-field checks

For every record, verify each required_fields entry for presence, verify each length_limits entry for range, and scan every field value for any forbidden_words.

Part 2 — Cross-field overlap

Using overlap_fields and ignored_words, compute the Jaccard ratio as described and flag records whose two name columns don't look related enough.

Example

config = {
    "required_fields": ["vendor_id", "vendor_name", "legal_name"],
    "length_limits": {"vendor_name": (4, 80), "legal_name": (4, 120)},
    "forbidden_words": ["test", "dummy"],
    "overlap_fields": ("vendor_name", "legal_name"),
    "ignored_words": {"inc", "llc", "ltd", "co", "corp", "company", "the"},
    "min_overlap_ratio": 0.5,
}
# Row 2 (missing vendor_name) -> {"row": 2, "issues": ["missing:vendor_name"]}
# Row 4 (vendor_name "Zed", length 3 < min 4) -> {"row": 4, "issues": ["length:vendor_name"]}

Constraints

  • Up to 10^4 rows and 50 columns.
  • Field values are printable text; forbidden words and ignored words match case-insensitively as whole words only.

Hints

Hint 1

Use csv.DictReader over io.StringIO so quoted commas are handled and columns missing from short rows come back as None.

Hint 2

Match banned words with a word-boundary regex (\b word \b, re.IGNORECASE) so 'test' doesn't fire inside 'attestation'.

Hint 3

Normalize both name columns identically — lowercase, non-alphanumerics to spaces, split, subtract ignored words — before computing Jaccard on the two sets.