Parse a Partial JSON Stream
Updated
Problem statement
The string prefix contains the part of a JSON stream received so far. Read this partial value and return completed JSON text with no whitespace outside strings.
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 the beginning of valid JSON. You do not need to repair malformed JSON.
Partial values
- Keep every string or array whose opening delimiter has arrived, including an empty partial value.
- If a string is unfinished, keep the literal content and complete escape atoms received so far, then add its closing 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 keeping those values, close every array that is still open. Ignore a final comma outside a string. Do not add a child value unless its opening quote or bracket has arrived.
- 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.
Worked example
The input ["alpha", ["be keeps the partial value ["alpha", ["be"]]. The output writes that value without whitespace outside strings.
Function
completePartialJson(prefix: String) → StringExamples
Example 1
prefix = "[\"alpha\", [\"be"return = "[\"alpha\",[\"be\"]]"The 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 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.
Similar questions at other companies
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Decode StringBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Parenthesized String CalculatorMeta · PHONE SCREENMeta interview questions
- Flatten JSON With Array IndicesAdobe · PHONE SCREENAdobe interview questions
- Decode StringSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
- Plus and Minus CalculatorIndeed · PHONE SCREENIndeed interview questions
- Basic CalculatorAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions