KatInterview

Rule-Driven CSV Validator with Cross-Field Word Overlap

StringsModel answer
MediumModel answerStringscsv-parsingvalidationstring-normalization

Last updated

Design and implement a configurable validator that flags per-field rule violations and excessive word overlap across fields in a company CSV.

A batch of company records arrives as CSV text: the first line is a header naming the columns, and each subsequent line is one company. Build a validator that checks each record against a configurable rule set and reports, per record, every rule it violates. Nothing is fixed: the required columns, min/max length limits, forbidden words, words to ignore during comparison, which columns are compared for overlap, and the overlap threshold are all passed in as configuration.

Example input (columns and values are illustrative):

id,name,legal_name,description
1,Acme Widgets,Acme Widgets LLC,Industrial widgets for factories
2,,Bright Labs LLC,
3, ACME widgets , acme widgets llc,Widgets

Two stages of work:

Stage 1 — Single-field checks. For each record, verify (a) every required field is present and non-empty, (b) each field under a length rule satisfies its min and/or max, and (c) no checked field contains a forbidden word. Each failure must be reported with the record's identifier, the field name, and which rule failed.

Stage 2 — Word overlap across fields. Tokenize several configured fields of the same record, fold case, collapse whitespace, strip punctuation per an agreed rule, and remove ignored words (e.g. LLC). Compute an overlap measure (count of shared tokens or a ratio) and flag records that violate the threshold in the configured direction (too little or too much sharing). Merge these failures into the Stage 1 report without double-counting.

Expected outputs must be deterministic: same input, same report order.

Things to nail down with the interviewer: whether whitespace-only counts as missing; whether length is measured on raw or trimmed values and whether limits are one-sided or two-sided; whether forbidden words match whole words or substrings and whether they can be multi-word phrases; which columns are compared and the exact overlap formula and threshold direction; punctuation handling; whether to report all failures or just the first; and the output shape (per-failure list, per-record pass/fail, or both).

Hints

Hint 1

Parse with a real CSV reader — quoted commas and unbalanced columns must be handled explicitly, not split on commas.

Hint 2

Normalize once, reuse everywhere: a single function that lowercases, trims, strips punctuation and drops ignored tokens keeps both stages consistent.

Hint 3

Define overlap precisely (e.g. Jaccard = shared / union) and state what the threshold direction means; then test empty-after-normalization fields, which typically should not fire an overlap failure.