← Questions
Validate a JSON-like string
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Microsoft's coding round, last asked Jan 2026. It tests parsing, stacks and validation.
Write a function that decides whether a string is well-formed JSON-like text. You are checking structure only: you do not need to build the parsed value.
The grammar to accept:
- A value is an object, an array, a string, a number,
true,falseornull. - An object is
{followed by zero or more"key": valuepairs separated by commas, then}. Keys must be strings. - An array is
[followed by zero or more values separated by commas, then]. - A string is enclosed in double quotes. Inside it, a backslash escapes the next character (
\",\\,\n,\t,\uXXXXand so on), so an escaped quote does not end the string. - Whitespace may appear between tokens. The whole input must be exactly one value.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
is_valid(text: str) -> bool— returnsTruewhentextis one complete, well-formed value, elseFalse. It never raises.
Example
'{"a": [1, 2.5, {"b": null}], "c": "x\"y"}' -> True
'[true, false]' -> True
'{"a": 1,}' -> False # trailing comma
'{a: 1}' -> False # key not quoted
'{"a" 1}' -> False # missing colon
'[1, 2' -> False # unclosed bracket
Constraints
-
0 <= len(text) <= 10^6. - Nesting can be deep (assume up to 10^5 levels unless told otherwise).
Clarifying questions
- Are numbers full JSON numbers (sign, fraction, exponent, no leading zeros), or any run of digits?
- Which escape sequences are legal, and must
\ube followed by exactly four hex digits? - Is the empty string valid? Is a bare scalar such as
42a valid document? - Can I use recursion, or could nesting be deep enough to overflow the call stack?
What a strong answer covers
- A tokenizer that handles strings and escapes first, so brackets and commas inside strings are ignored.
- Either a recursive-descent parser (one function per grammar rule) or an explicit stack of open containers plus an "expected next token" state. Name the trade-off: recursion is clearer, the stack survives deep nesting.
- Comma and colon placement: no leading, trailing or doubled commas, every key followed by a colon.
- O(n) time; O(depth) space.
- Tests for each rule: empty containers, nested containers, escaped quotes, trailing garbage after a valid value.
Follow-ups
- Report the position and reason of the first error, not just
False. - Validate a stream that arrives in chunks without holding the whole text in memory.
- Allow comments or trailing commas as an option, and keep the code clean.
Where it's asked
- Microsoft · Coding · Last asked Jan 202660 min · Ten to fifteen minutes of behavioral questions, then coding in HackerRank where you write a main and make the tests pass; usually three rounds · No AI unless the interviewer invites it; some 2026 loops add a round where you bring your own AI agent
What it tests
parsing · stacks · validation
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
