KatInterview

Harden a Messy Text Ingestor and Triage a Broken ML Training Repo

Machine LearningModel answer
MediumModel answerMachine Learningdebuggingdata-parsingdata-leakage

Last updated

Design a fault-tolerant parser for noisy text records, hunt down three hidden bugs in a training codebase, and plan how to verify everything within a one-hour budget.

You are given a small machine learning project and a pile of raw text files that feed it. You have 60 minutes. The work has three parts.

Part 1: Ingest messy text

The raw files contain delimited records with inconsistent formatting. Design and implement a parser that turns them into structured examples. It must cope with:

  • Delimiters and quoted fields, including delimiters that appear inside quotes.
  • Escaping (doubled quotes and backslash escapes) and newlines inside quoted fields.
  • Mixed encodings and Unicode quirks (BOMs, invalid byte sequences, combining characters, mixed line endings).
  • Missing fields, extra fields, trailing delimiters, blank lines, and repeated header rows.
  • Malformed lines, which must not crash the run and must not vanish silently.

Explain the output format, how you distinguish a missing value from an explicitly empty one, and how you would test the parser.

Part 2: Fix three defects

The project has a data loader, a preprocessing/tokenization step, a training loop, and an evaluation script. Three defects are hidden in it. Typical culprits include an off-by-one in tokenization or indexing, train/test leakage, a wrong loss reduction, nondeterministic seeding, and tensor shape mismatches.

Describe how you would find and fix them quickly in code you have never seen: how you would use stack traces, assertions, binary-search logging, and minimal reproductions.

Part 3: Prove it works

Explain how you would validate your fixes within the time limit: unit tests, an end-to-end run, metric sanity checks, and regression guards.

Provide a structured answer with concrete code for the parser and specific checks for the debugging and validation phases.

Hints

Hint 1

Split the parser into three stages (decode bytes, tokenize into fields, validate against a schema) so every failure mode lives in exactly one place and can be tested alone.

Hint 2

Never let a bad line raise. Collect it in an error list with line number, reason, and a truncated snippet, and cap how many lines a quoted field may span so one stray quote cannot swallow the file.

Hint 3

For debugging, shrink first: tiny batch, one step, no worker processes. Then assert invariants at stage boundaries (id range vs vocab size, logits vs label shapes, disjoint train/test ids).

Hint 4

Sanity metrics catch what asserts miss: overfit a handful of examples to near-zero loss, check initial loss is near log(num_classes), and confirm two runs with the same seed match.