A practice prompt we wrote. No company or candidate report names it, so it carries no company tag.
How to answer
“Flatten” tempts you toward a generic recursive function. Don’t write one. The job is to map three known shapes onto one target and fail loudly on any shape you didn’t map.
- Ask for one real payload per version, and write the target first. Give each field its type and unit: string ids, money in minor units, UTC timestamps, and which fields may be null. Decide what each array becomes: a child table keyed by the parent id and position, never columns like
item_0_sku. The target is the contract. - Detect the version from a field, not the shape. If the oldest version has no version field, identify it by keys only it has, and let the unmapped-field check reject anything extra. An unknown version raises, never mapped by the nearest match.
- Write one mapping function per version. Each line reads a path in that version and fills a target field, so v4 becomes a new function, not a branch in an old one.
- Separate missing, optional and wrong. A missing required field raises with the version and path. A missing optional field becomes
None. A wrong type raises; in PythonTrueis anint. - Account for every field. Record the paths each mapping read and list the rest, so a field renamed upstream shows up as an unmapped path, not a column of nulls. Ask whether unmapped fields stop the load or go in a report.
- Ask about units the payload doesn’t state: the time zone of old timestamps, the currency of an amount with no currency field. Put each answer in a named constant and note who confirmed it.
The trap is .get("email") everywhere. It never raises, so when the vendor renames email, every row loads with a null email and nothing tells you.
Follow-ups
What the interviewer may ask next, once your first answer is on the table.
- The vendor ships a minor release that adds a field. Does your loader stop, and should it?
- The oldest version has timestamps with no offset. How do you find out what they mean, and what do you do about the hour that happens twice each autumn?
- A payload arrives with no version field and a shape you have never seen. What happens to it, and who hears about it?
Where answers go wrong
- Writing a generic recursive flattener, so the same customer name lands in a different column for each version and a rename upstream becomes a new column nobody reads.
- Chaining get calls with defaults, so a renamed field quietly fills the target column with nulls instead of failing.
Answer this in two minutes
Write the answer you would say out loud. The clock starts with your first word.
Model answer
“Here’s what I understood from the three samples. v1 is flat, with the total as a dollar string, a local timestamp, no version field and no line items. v2 nests under order, sends integer cents with a currency, an ISO timestamp and an items array, and has version: 2. v3 nests deeper under data, sends epoch milliseconds and a lines array, and has api_version: "3.1". The target is one flat order record, plus a second table for line items keyed by order_id and position.