A practice prompt we wrote. No company or candidate report names it, so it carries no company tag.
How to answer
This question is about the reader of the output. Say so: success is that a stranger fixes every problem in one pass from the messages alone.
- Pin the rules and the reader. Format, required keys, types and ranges, and whether unknown keys are errors or warnings. Ask who runs it, a customer engineer or a CI job: that decides the output format and exit code.
- Separate parsing from validation. An unparseable file gets one error with the parser’s line and column, and no validation.
- Write the rules as data. A small schema of paths, types, ranges and allowed values, walked by one function. Rules that span fields, such as “
client_secretis required whenauthisoauth”, run after the per-field pass, on every input that parsed. If a broken input forces a skip, print that (auth: oauth checks skipped until auth is valid), so the second run holds no surprise. - Collect, never raise. Each error carries a path, a line where you have one, what was expected, what was found and a fix:
config.yaml:14 database.port: expected an integer from 1 to 65535, found "54 32". Remove the space.Report a section of the wrong type once and skip its children. For an unknown key, suggest the closest known one:protbeside a missingportgets “did you mean port?” - Print in file order and exit non-zero. Group by section and end with a count. Never echo a value from a field marked secret, or from an unknown key:
paswordis not marked secret, and its value is the password.
Line numbers need a parser that keeps positions, such as a YAML node tree; a plain dict loses them. The trap is stopping at the first error, which turns every config change into a rerun loop.
Follow-ups
What the interviewer may ask next, once your first answer is on the table.
- The user fixes the first error and reruns, and a new error appears that you could have reported the first time. How do you stop that?
- Your parser returns a plain dict. How do you still report a line number?
- The invalid value is a database password. What does your message print?
- Two fields are each valid on their own but wrong together. Where does that rule live, and when does it run?
Where answers go wrong
- Raising on the first error, so the user fixes and reruns once per mistake.
- Printing the exception, such as a bare KeyError on a key name, instead of the path, what was expected, what was found and how to fix it.
- Reporting every child of a section that is the wrong type as missing, so one mistake produces a screen of errors.
Answer this in two minutes
Write the answer you would say out loud. The clock starts with your first word.
Model answer
“Success is that a stranger fixes every problem in one pass, from the messages alone. So before code: the file is YAML for one service, with three sections.