FastPrepJSON Parser
Problems/JSON Parser

JSON Parser

Patreon logoPatreon● HardFULLTIMEPHONE SCREEN

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: One report gives this task: a personal blog post from June 2021 in which a candidate who had worked for six or more years writes about his senior software engineer interviews. The post lists his phone screens one by one. The candidate lists this phone screen under the name Patron; we read it as Patreon, which is among the companies he applied to. That entry says the round was a very long implementation for parsing JSON, that in his view the interviewer had confused him partway, and that he did not finish and was rejected. The stage is the phone screen because the post lists this round among its phone screens. Our choices: the task is to write the parser yourself without a JSON library; the function receives the JSON text as one string and returns the same value written in one fixed form, with no whitespace outside strings, array items kept in order, object members sorted by their decoded keys, numbers copied exactly as written, and strings written again with a fixed set of escapes and lowercase hex codes; the input is always valid JSON made of printable ASCII characters plus tab, line feed and carriage return outside strings, with no repeated keys in an object and no \u escapes for codes D800 to DFFF. The length limit of 2,000 characters and the nesting limit of 100 levels are ours, and all three examples are ours.

Write a JSON parser. The string text holds one JSON value. Read it and return the same value written out in a single fixed form, so that two texts that differ only in spacing, in the order of object keys or in how string characters are escaped give the same answer.

A JSON value is one of these:

  • an object: {, then zero or more members separated by commas, then }. A member is a string key, a colon and a value;
  • an array: [, then zero or more values separated by commas, then ];
  • a string: characters between double quotes, where a backslash starts an escape: \", \\, \/, \b, \f, \n, \r, \t. An escape can also be \u followed by 4 hex digits, which give a character code;
  • a number: an optional -, then 0 or digits that do not start with 0, then optionally . and one or more digits, then optionally e or E, an optional + or - and one or more digits;
  • true, false or null.

Spaces, tabs, line feeds and carriage returns may appear before and after any value, key, colon or comma, and between the brackets of an empty array or the braces of an empty object, as in [ ] and { }.

Assumption: the fixed form is written like this:

  • There is no whitespace anywhere outside strings.
  • Array items appear in the same order as in text.
  • The members of every object, at every level, are sorted by key. Two keys are compared after their escapes are turned back into characters, character by character, by character code (the character's Unicode value), so "B" comes before "a". If one key is the start of the other, the shorter key comes first, and the empty key comes before every other key.
  • A number is copied exactly as it is written in text, so 1.0, -0 and 2E+3 do not change.
  • true, false and null are written as they are.
  • Every string, key or value, is written between double quotes from its characters. A double quote becomes \" and a backslash becomes \\. The characters with codes 8, 12, 10, 13 and 9 become \b, \f, \n, \r and \t. Every other character whose code is below 32, or is 127 or more, becomes \u followed by its code as 4 lowercase hex digits, so the character with code 233 becomes \u00e9. All remaining characters, / included, are written as themselves.

Assumption: text is always valid JSON with nothing but whitespace around its one value. Every character of text is a printable ASCII character (code 32 to 126), a tab, a line feed or a carriage return, and the last three never appear inside a string. A \u escape may use upper or lower case hex digits and never gives a code from D800 to DFFF (hex). No object has two keys that are equal once their escapes are turned back into characters.

Assumption: write the parser yourself; do not use a JSON library from your language.

Function

parseJson(text: String) → String

Examples

Example 1

text = "{ \"zip\": \"02139\", \"Zone\" : [ 3, -0.50, 1E+2 ],\n\t\"id\": {} }"return = "{\"Zone\":[3,-0.50,1E+2],\"id\":{},\"zip\":\"02139\"}"

The spaces, the line feed and the tab between tokens are dropped. The keys are sorted by character code: Z (90) comes before i (105), which comes before z (122). The numbers -0.50 and 1E+2 are copied as written, and "02139" is a string, so its leading zero stays. The empty object becomes {}.

Example 2

text = "[\"tab\\there\", \"\\u0041\\u00E9\\/\", \"say \\\"hi\\\"\\\\\"]"return = "[\"tab\\there\",\"A\\u00e9/\",\"say \\\"hi\\\"\\\\\"]"

Written without the outer quotes of the input string, text is ["tab\there", "\u0041\u00E9\/", "say \"hi\"\\"]. The tab escape stays \t. \u0041 is the character A, which is written as itself; \u00E9 has code 233, so it is written again as \u00e9 with lowercase hex digits; \/ becomes a plain /. In the last string the quotes and the backslash keep their escapes.

Example 3

text = "{\"\\u0062\": 1, \"a\\n\": 2, \"a\": 3, \"~\": [[], {\"z\": null}]}"return = "{\"a\":3,\"a\\n\":2,\"b\":1,\"~\":[[],{\"z\":null}]}"

Written without the outer quotes of the input string, text is {"\u0062": 1, "a\n": 2, "a": 3, "~": [[], {"z": null}]}. The first key decodes to b. The key a is the start of the key a followed by a line feed, so it comes first; the line feed key comes before b because a (97) is smaller than b (98). ~ has code 126, so its member is last. The array keeps its order, and the object inside it is written in the same form.

Constraints

  • 1 <= text.length <= 2000
  • text is valid JSON that follows the input assumption above.
  • Arrays and objects are nested at most 100 levels deep: the outermost array or object is level 1, and an array or object directly inside a level k one is level k + 1.

More Patreon Phone Screen questions

  • Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
  • Uniform Random Number from Coin TossesPHONE SCREEN · Seen Feb 2026
  • Wordle Guess CheckerPHONE SCREEN · Seen Jan 2026
  • Org Chart Report CountsPHONE SCREEN · Seen Dec 2025
  • Format the Missing Numbers from 1 to 99PHONE SCREEN · Seen Sep 2025
  • Pythagorean Triplets in an ArrayPHONE SCREEN · Seen Oct 2024
  • LRU Cache with TTLPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Password GeneratorPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
  • Merge IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2022

Similar questions at other companies

  • Arithmetic Expression EvaluatorMicrosoft · PHONE SCREENMicrosoft interview questions
  • Toy Language Generic Return-Type InferenceOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Restricted JSON ParserOld Mission · PHONE SCREEN
  • Basic Calculator with Multiplication and ParenthesesBloomberg LP · PHONE SCREENBloomberg LP interview questions
  • Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
  • Top K Movies by Play Count from CSV DataTubi · PHONE SCREEN
  • Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
  • Parse Nested Message TypesApplied Intuition · PHONE SCREENApplied Intuition interview questions
  • Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
  • Summarize Server Healthcheck LogsNVIDIA · PHONE SCREENNVIDIA interview questions
All Patreon interview questionsSee Patreon hiring insights
Saved
class Solution {
    public String parseJson(String text) {
        // Write your code here.
    }
}
Checking account…