Parse a Partial JSON Stream
Problem statement
A JSON stream has arrived only as the string prefix. Parse the partial value already received and return its completed compact JSON text.
For this exercise, assume the stream contains exactly one value built from strings and arrays of such values. The root may be a string or an array. Objects, numbers, booleans and null are outside the exercise. The input is guaranteed to be a valid prefix, not malformed JSON requiring general error recovery.
Partial values
- Keep every string or array whose opening delimiter has arrived, including an empty partial value.
- If a string is unfinished, retain its received literal content and complete escape atoms, then close its quote.
- An escape atom is a backslash followed by one of
" \ / b f n r t, or a backslash followed byuand four hexadecimal digits. If the input ends inside an atom, omit that whole final atom. Do not invent its missing characters. - After retaining those values, close every array that is still open. Ignore a final comma outside a string: a child whose opening delimiter never arrived does not exist.
- If no root value has started because the input is empty or contains only whitespace, return the text
[].
Exact output text
Remove spaces, tabs, carriage returns and line feeds only when they occur outside strings. Inside a string, preserve literal characters and the exact spelling of complete escape atoms; do not decode or re-encode escapes, normalize text or combine Unicode escape code units. Return the completed JSON text as a String, not a nested runtime object.
What the interview report shared
The reported example receives ["alpha", ["be and retains the partial value ["alpha", ["be"]]. The practice output serializes that same value compactly.
Function
completePartialJson(prefix: String) → StringExamples
Example 1
prefix = "[\"alpha\", [\"be"return = "[\"alpha\",[\"be\"]]"The reported prefix has a complete first string and an unfinished string inside an open nested array. Keep be, then close its quote and both arrays. The returned text uses the exercise’s compact serialization.
Example 2
prefix = "[\"red\","return = "[\"red\"]"The final comma has not started another element. Remove that separator and close the array; do not add an empty string or another array.
Example 3
prefix = "\"line\\u00"return = "\"line\""The final Unicode escape has fewer than four hex digits, so its entire incomplete atom is omitted. Keep the received literal content line and close the root string.
Constraints
0 <= prefix.length <= 2000, measured in Unicode scalar input characters; escape syntax counts as its literal ASCII characters.- At most
200arrays are nested at once. prefixis a prefix of one value containing only JSON strings and arrays. Outside-string whitespace is limited to space, tab, carriage return and line feed.- Literal Unicode characters arrive at complete scalar boundaries. Unicode escape atoms use exactly four hex digits; completed code-unit escapes retain their raw spelling.