The Convert JSON String to Object interview question is a classic parsing problem that requires you to implement a custom JSON parser. You are given a string representing a JSON object and must return its equivalent data structure (like a Map or Dictionary in your language of choice). This includes handling various data types such as nested objects, arrays, strings, numbers, booleans, and null values. Unlike using a built-in library like JSON.parse() in JavaScript, this task forces you to manually manage the state of the parser as it traverses the characters.
Companies like MongoDB and Verizon ask the Convert JSON String to Object coding problem to assess a candidate's mastery of recursion and state machines. It’s a "Hard" problem because it requires extreme attention to detail. You must handle complex scenarios like escaped characters in strings, floating-point numbers, and deeply nested structures. It demonstrates your ability to write robust, error-resistant code that can handle malformed input gracefully.
The primary interview pattern for this problem is a Recursive Descent Parser. You maintain a pointer (or index) to the current character being processed. Depending on what character the pointer is on, you call a specialized function:
{ calls parseObject()[ calls parseArray()" calls parseString()t, f, or n call parseLiteral() (for true, false, null)- calls parseNumber()Input: {"id": 101, "tags": ["tech", "coding"]}
{, identifying an object."id". parseString() is called to extract the key "id".:, then 101. parseNumber() is called to convert the string "101" to an integer.,, then "tags". parseString() extracts the key "tags".:, then [. parseArray() is called.parseArray(), it sees "tech", calls parseString(), and adds it to a list."coding", then sees ], finishing the array.}, finishing the object." or `` inside a string correctly.-12.34).Practice implementing a smaller version first—just strings and numbers. Once you have the basic recursive structure, adding support for arrays and objects becomes much easier.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Design Search Autocomplete System | Hard | Solve | |
| Text Justification | Hard | Solve | |
| Sliding Window Maximum | Hard | Solve | |
| Merge k Sorted Lists | Hard | Solve | |
| 24 Game | Hard | Solve |