Recover Valid JSON From a Truncated Streaming Model Response

Last updated: November 29, 2026

Quick Overview

A model streams a JSON object and the stream is cut off at an arbitrary character. Write a function that returns the longest valid JSON document recoverable from the prefix: keep complete values, close a partial string value, and drop dangling keys, commas and partial literals.

Ode
Coding & Algorithms
Software Engineer
Ode
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A model streams a JSON object and the stream is cut off at an arbitrary character. Write a function that returns the longest valid JSON document recoverable from the prefix: keep complete values, close a partial string value, and drop dangling keys, commas and partial literals.

Ode builds client UIs that render structured model output as it streams (an extraction form filling in field by field), and outputs get truncated by max-token limits. This onsite problem checks whether the candidate can write a small hand-rolled parser with an explicit state machine and reason about which cut points are safe.

What the Interviewer Expects
  • Track a stack of open containers and, for objects, whether a key, colon or value is expected
  • Record a safe cut position each time a value completes or a container opens
  • Close a truncated string only when it is a value, never when it is a key
  • Treat partial literals and partial numbers like `tru` or `1.` as unsafe
  • Handle escapes, including a dangling backslash at the cut
  • Linear time in the prefix length
Key Topics to Cover
parsing
state machine
stack
streaming
structured output
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you make this incremental so each new chunk costs O(chunk) instead of re-parsing?
  • Should a cut number like `12` (from `125`) be shown to users? How would you flag it?
  • How would you combine this with schema validation to know which fields are final?
  • How does constrained decoding or a tool-call API change the need for this?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input is a prefix of some valid JSON text. Output is a valid JSON string that is the prefix up to the last safe point plus the needed closers, or a partial string value closed with a quote. Return `No...

High-Level Design

Scan once. The stack holds [bracket, state] where state is key, colon, value or after_value. Two kinds of safe points: right after { or [ (an empty container is valid) and right after a ...


Submit Your Answer
Markdown supported

Related Questions