# Messy Data Into Records With Low-Confidence Flags

> How to turn a messy take-home data file into clean records, with flags on every value you are not sure of, explained for your AI interview.

**In short:** Read the messy export with rules wherever a rule can check the value, and use a model only for free-text notes. Each value that cannot be confirmed gets a flag with its reason and goes to a person, never a quiet guess.

AI and LLM interview question Q398, Forward Deployed Engineering and applied AI. Intermediate, common (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/take-home-messy-data-records-with-low-confidence-flags

## Say it in 30 seconds

A take-home like this gives you a messy file and asks for clean records. I'd write a schema first, so every field has a type. Rules in code read the dates, numbers and ids, because a rule can check them. A model only reads the free-text notes. Anything I can't confirm gets a flag with a reason and goes to a person. I never fill a gap quietly. Then I'd measure the flags on a small hand-checked sample, so I can say how often they're right.

## Say it in 2 minutes

A take-home like this gives you a messy export and asks for clean records back. A record is one row per thing, with every field in the right type. Before writing any code, I'd write the schema: the fields, their types, which ones can be empty, and the allowed values. I'd keep the raw text beside each parsed value, so anyone can see what was actually typed.

Then I'd split the work by what can be checked. Dates, numbers and ids go through rules in code. A rule can tell that a date like 30 February is impossible, or that an id appears twice. A model only reads the free-text notes, where rules can't help. Each row gets a list of flags, and each flag gives its reason, such as a weight that isn't a number. I never write a guess as if it were a reading. A guessed value gets its own flag.

Flagged rows go to a review queue with the raw line beside them, so a person can decide in seconds. I wouldn't trust a model's confidence number on its own. Those scores only mean something if they're calibrated, and you can only check that against labelled examples.

So I'd hand-label a small sample, count the flags that were right, and count the errors that slipped through. Then I'd report both numbers in the README, with the limits, so the reviewer sees where the work is uncertain.

## What is a take-home like this, in plain words?

A take-home like this gives you a messy file and asks for clean records back. A record is one row per thing, with each field in the right type: an id is text, a weight is a number, a date is a real date. Messy means typos, mixed date formats, repeated rows and blank cells.

Think of a claims clerk at an insurer who types handwritten forms into a system. The clerk enters what they can read and check. For anything unclear, they stick a note on the row saying what is wrong, and a senior clerk looks at it. They do not guess quietly.

The made-up case here is a freight company with 12,000 delivery notes from three depot systems. Drivers typed the notes, and each depot exports them in its own format. The reporting team needs one clean record per shipment.

- **Schema:** The list of fields for each record, with their types, allowed values and whether they can be empty.
- **Flag:** A note on one value saying why it is not trusted, such as an impossible date.
- **Provenance:** Where a value came from: the file, the line and the raw text as it was typed.
- **Review queue:** The flagged rows a person checks, with the reason shown beside each one.
- **Free text:** Words typed by people, like a driver's note, which rules cannot check well.

## What should the records look like?

Write the schema first. Every field has a type, a rule for empty values, and a list of allowed values where one applies. Keep the raw text beside each parsed value. Without it, a reviewer cannot see what was typed, and cannot tell a parsing mistake from a real error.

- **Loose: Store whatever came in.** Keep every field as text and clean it later. Nobody can say which rows were checked.
- **Schema first: Typed fields, raw copy kept.** Each field has a type and a rule. Each value keeps its raw text, so a flag can point at it.

Use rules wherever a rule can check the value. OpenAI says its structured outputs constrain the model's output to a JSON schema you supply, and that your application still has to validate the business rules the schema does not express. The take-home works the same way: the schema fixes the shape, and code checks the rules.

> "Your application still needs to handle refusals, incomplete responses, and API errors, and validate any business rules that the schema does not express." (OpenAI)
>
> "When imputing missing values, use reliable methods like mean/median imputation and consider adding an indicator column to signal imputed values to the model." (Google)

*Diagram, From a messy export to records: The export goes to two readers: rules for the dates, numbers and ids, and a model for the notes. Both reach one set of checks in code. Passing rows become clean records, and a failed check sends the row to a person with its reason. The numbers are made up.*
1. Each line is read by fixed rules.
2. The free-text notes also go to a model.
3. Parsed fields go on to the checks.
4. The model's reading goes to the same checks.
5. A failed check sends the row to a person, with the reason.
6. A row that passes every check becomes a clean record.

Google's ML course lists the usual problems in a dataset: omitted values, duplicate examples, out-of-range values and incorrect labels, which can hurt model performance. In a take-home, omitted values, duplicates and out-of-range values show up as flags. Incorrect labels only show up when someone checks a sample by hand.

## How do you decide what gets flagged?

Flags come from evidence you can check. These are the checks I'd run, in order. Each one either passes or adds a flag with its reason.

- **Parse each field by rule:** Read dates, numbers and ids with a fixed rule. If the rule fails, keep the raw text and flag the field. The date rule flags every slash date, even 13/03/2026, because the text alone does not say which part is the day.
- **Check inside the row:** A date can have the right shape and still be impossible. 30 February fails the calendar, so the rule flags it.
- **Check across rows:** A repeated id is a duplicate, even when every value in the repeat looks fine.
- **Mark every guess:** If you fill a gap, store the guess with a flag saying it was filled. Google's crash course suggests the same idea for training data: an extra column that marks imputed values.
- **Send flags to a person:** The review queue shows the row, its flags and the raw text together, so the fix takes seconds.

Passing checks mean the rules held. They do not prove the record is true. That is why the sample check later matters. Here is the rule-based part in plain Python. Five made-up rows go in, and each one gets flags or passes.

```python
import re
from datetime import date

# Made-up delivery notes from three depot exports, before any cleaning.
RAW = """SH-1041 | 12.5 kg | 2026-03-04 | Leeds
SH-1042 | 1,2O0 kg | 2026-03-05 | Leeds
SH-1043 | 8 kg | 05/03/2026 | Bristol
SH-1041 | 12.5 kg | 2026-03-04 | Leeds
SH-1044 | 40 kg | 2026-02-30 | Leeds"""

records, seen = [], set()
for line in RAW.splitlines():
    sid, weight, day, depot = [part.strip() for part in line.split("|")]
    flags = []
    if sid in seen:
        flags.append("duplicate id")
    seen.add(sid)
    kg = weight.replace(" kg", "").replace(",", "")
    if not re.fullmatch(r"\d+(\.\d+)?", kg):
        flags.append("weight is not a number")
    if re.fullmatch(r"\d{2}/\d{2}/\d{4}", day):
        flags.append("ambiguous date")
    else:
        try:
            date.fromisoformat(day)
        except ValueError:
            flags.append("impossible date")
    records.append((sid, kg, day, depot, flags or ["ok"]))
for sid, kg, day, depot, flags in records:
    print(sid, "|", "; ".join(flags))
```

Output:

```text
SH-1041 | ok
SH-1042 | weight is not a number
SH-1043 | ambiguous date
SH-1041 | duplicate id
SH-1044 | impossible date
```

Notice the second SH-1041 row. Its values look fine on their own. Only the check across rows catches it.

## Why not trust the model's own confidence number?

Some models give a score next to each answer. Treat it with care. A score is only worth something if the model is calibrated, and scikit-learn's guide says a well calibrated classifier's output can be read directly as a confidence level. You can only check calibration against labelled examples.

> "For scenarios where accuracy is critical, confidence can be used to determine whether to automatically accept the prediction or flag it for human review." (Microsoft Azure)
>
> "Well calibrated classifiers are probabilistic classifiers for which the output of the predict_proba method can be directly interpreted as a confidence level." (scikit-learn)

Azure's own guide adds a warning: its accuracy can be inconsistent when your documents differ from the ones it was trained on. So a flag cannot lean only on a score the model gives. Anthropic's guide takes a similar line for language models: it says to give the model permission to admit uncertainty, and in its press-release example the model must retract any claim it cannot back with a quote from the source documents.

So I'd measure the flags myself. Here is a made-up result for 200 hand-checked rows. 40 rows were flagged, and 31 of those were really wrong, so 9 flags were false alarms. Six wrong rows were never flagged at all.

*Diagram, Checking the flags on a sample: Made-up numbers from a 200-row sample. The flags and the missed errors both feed the rule changes, then the same sample is measured again. Two numbers go in the report: how many flags were right, and how many errors slipped past.*
1. Compare every flag with the hand label.
2. Look for errors that nobody flagged.
3. Fix rules that flag too much.
4. Fix checks that let errors through.
5. The measured results become the two numbers in the report.

Report both numbers. Tuning the rules usually moves one against the other: stricter rules flag more and miss less, but they add work for the reviewer. Say which way you chose and why.

### How would you present this to the reviewer?

Send a short README with the code. A reviewer wants to see where the work is uncertain more than how clever it is.

- **Counts:** Rows in, rows clean, rows flagged, and flags by reason. Reviewers check the numbers first.
- **Trade-offs:** What the rules read, what the model read, and why you split them that way.
- **Limits:** What the sample did not cover, such as a depot format you never saw, and what you would check with the team next.

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] A schema first: fields, types, empty rules, and the raw text kept beside each value.
- [ ] Rules for anything a rule can check. A model only for free text.
- [ ] Flags with reasons, never a guess written as a fact.
- [ ] Checks across rows as well as inside one row.
- [ ] A small hand-labelled sample to measure the flags.
- [ ] A README with counts, trade-offs and limits.

**Common traps**

- Trap: "The model cleans the whole file." Better: Use code for what a rule can check. Send only the free-text notes to the model.
- Trap: "If it looks fine, it is correct." Better: A passing check means the rule held. Only a labelled sample shows the error rate.
- Trap: "I filled the gaps with the most likely value." Better: Mark each guess with a flag and keep the raw text, so nobody mistakes it for a reading.

## Can you spot the problem?

**Spot the best call:** A driver typed 05/03/2026 on a delivery note. Which handling is the best call?

A. Store 2026-03-05 as the date, with no flag.
B. Flag it as an ambiguous date, keep the raw text, and send it to review.
C. Store 2026-05-03 because the depot's format is probably US style.
D. Drop the row, because one date cannot be trusted.

**Answer:** B. The text could mean 5 March or 3 May, so a rule cannot decide. Flag it, keep the raw text, and let a person check. The other three either guess or silently lose a real delivery, and nobody sees the change.

**Quick check:** Your checks pass for 98 per cent of rows. What does that prove?

A. The records are correct.
B. The rules held on those rows. The real error rate is still unknown until a sample is hand-checked.
C. The model's confidence scores are high.
D. Every duplicate has been removed.

**Answer:** B. Passing checks show the rules held. Only hand-checking a sample shows how many errors got through, because some errors pass every rule.

## Can you answer these from memory?

**Flash cards**

- **What is a flag?** A note on one value saying why it is not trusted, so a person can check it.
- **Where do dates and ids get checked?** In code, by rule. A model is only for free text such as notes.
- **What do you do with a guessed value?** Store the guess with a flag and keep the raw text. Never write it as a reading.
- **Is a model's confidence score enough?** No. It must be calibrated, which you check on a hand-labelled sample.
- **What does a passing check prove?** The rule held on that row. It does not prove the record is true.
- **What goes in a take-home README?** The schema, counts of clean and flagged rows, trade-offs and limits.

## Likely follow-up questions

- Q373 Design invoice extraction. https://designervenkat.online/interview/ai/design-invoice-extraction
- Q387 How do you deal with messy customer data? https://designervenkat.online/interview/ai/how-do-you-deal-with-messy-customer-data
- Q399 Parse a messy CSV/JSON file with edge cases. https://designervenkat.online/interview/ai/parse-a-messy-csv-json-file-with-edge-cases
- Q448 How do you extract structured data from documents and images? https://designervenkat.online/interview/ai/extract-structured-data-from-documents-and-images
- Q478 Parse and validate LLM JSON, retrying on failure. https://designervenkat.online/interview/ai/parse-and-validate-llm-json-retrying-on-failure
- Q172 When and how do you add a human in the loop? https://designervenkat.online/interview/ai/human-in-the-loop-for-ai-agents

## Learn it in depth

- [Data cleaning: fixing a messy table](https://designervenkat.online/learn/machine-learning/data-cleaning)
- [Structured Output](https://designervenkat.online/learn/ai-engineering/structured-output)
