A practice prompt we wrote. No company or candidate report names it, so it carries no company tag.

How to answer

The parsing is the easy half. The grade is a memory bound you can defend, and what your code does at chunk boundaries and at a truncated end.

  1. Ask for the shape first. JSON Lines, one document per line, needs nothing more than reading line by line. One top-level array means splitting it into elements. Records inside a wrapper object need a path. One enormous value needs an event parser. Ask what happens to each record, because the consumer sets the pace.
  2. State the contract. Memory is bounded by the largest element, not the file, and an element over a set cap is an error. Without the cap, an unclosed string buffers the rest of the file.
  3. Name the library, then build the core. In Python, ijson does this in production. If asked to write it, track only the grammar that decides where an element ends: inside a string or not, a pending backslash, and a stack of open brackets. Hand each complete element to json.loads.
  4. Scan bytes, not decoded text. A chunk can end mid-string, right after a backslash, or inside a multi-byte character. In UTF-8 every byte of a multi-byte character has its high bit set (RFC 3629, section 3), so none can look like a quote or a bracket.
  5. Fail loudly with a byte offset. A stream that ends before ] is truncated, not finished. Say which errors let you skip one element and which stop the stream.
  6. Test at every chunk size against json.loads on the whole document, and measure peak memory on a generated stream.

The trap is a brace counter that ignores strings: the first } inside a note ends the record early.

The streaming JSON array drill asks for the same splitter in TypeScript.

Follow-ups

What the interviewer may ask next, once your first answer is on the table.

  • The records sit in an orders array inside a wrapper object, not in a top-level array. What changes?
  • A single element is itself too large for memory. Does your design still work, and what would?
  • One element in the middle is malformed. Do you stop the load or skip it, and when can you not skip it?
  • The download dies partway through. How does the loader know, and where does a restart pick up?
  • The consumer writes to a database slower than you can parse. Where does the waiting happen?

Where answers go wrong

  • Calling json.load(f), which builds the whole document in memory before returning anything.
  • Reading a pretty-printed array line by line, which only works when the file happens to be JSON Lines.
  • Counting braces without tracking strings and escapes, so a } inside a note, or a backslash right before a quote, ends the record in the wrong place.
  • Decoding each chunk as UTF-8 on its own, so a character split across two chunks raises or turns into replacement characters.
  • Treating the end of the input as the end of the array, so a truncated download loads as a short, silent success.

Answer this in two minutes

Write the answer you would say out loud. The clock starts with your first word.

Two minutes

Model answer

“First, the shape, because it changes the answer. If it were JSON Lines, I’d read it line by line and stop there; it’s also what I’d ask the producer for if we could.