← Questions

Validate a JSON-like string

0
MicrosoftCodingLast asked Jan 2026

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, false or null.
  • An object is { followed by zero or more "key": value pairs 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, \uXXXX and 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 — returns True when text is one complete, well-formed value, else False. 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 \u be followed by exactly four hex digits?
  • Is the empty string valid? Is a bare scalar such as 42 a 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

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