← Microsoft Interview Insights

Microsoft·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Apr 2026

Summary

Microsoft SWE interview that went deep on parsing fundamentals. One meaty coding problem that turned into a longer design discussion than I expected.

Questions Asked (1)

Q1

Write a function that checks whether a given string is valid JSON. It should handle objects, arrays, strings with escape sequences, numbers, booleans, and null. Walk through your tokenizer design and parser approach, and cover edge cases like trailing commas, unterminated strings, and recursion depth.

Algorithms & Data StructuresTechnical Trade-offsSystem Design
Author's notes

Started with a regex and the interviewer just kind of waited.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the JSON specification and constraints, then outline a two-phase approach: a tokenizer that converts the input string into a stream of tokens, and a recursive descent parser that validates the token sequence against JSON grammar. Walk through the design for each component, highlighting how you handle edge cases like trailing commas, unterminated strings, and recursion depth, and discuss trade-offs between iterative and recursive parsing.

Pro tip: Mention that you would use an explicit stack or depth counter to avoid stack overflow on deeply nested structures, and that you'd validate numbers strictly according to JSON spec (no leading zeros, no hex, etc.) to show attention to detail.

1. Clarify requirements and constraints

Ask whether the function should return a boolean or throw errors, and confirm the JSON specification version (e.g., RFC 8259). Discuss input size limits and recursion depth expectations.

2. Design the tokenizer

Explain how to scan the string character by character, producing tokens for structural characters, strings (handling escape sequences), numbers, and literals (true, false, null). Mention skipping whitespace and detecting invalid characters.

3. Design the parser

Describe a recursive descent parser that consumes tokens and validates the grammar: value can be object, array, string, number, boolean, or null. For objects and arrays, handle nested structures and enforce correct comma/colon placement.

4. Address edge cases

Discuss how to detect trailing commas (by checking token sequence), unterminated strings (by ensuring closing quote), and recursion depth (by using an explicit stack or depth limit). Also cover invalid escapes, leading zeros in numbers, and duplicate keys.

5. Discuss trade-offs and optimizations

Compare recursive vs iterative parsing, tokenizer vs single-pass parsing, and error reporting strategies. Mention potential optimizations like early termination and memory usage considerations.

Key Points to Mention

  • Token types: structural characters ({, }, [, ], :, ,), strings, numbers, booleans, null
  • String escape sequences: \", \\, \/, \b, \f, \n, \r, \t, \uXXXX
  • Number validation: no leading zeros, optional fraction and exponent, no hex or Infinity/NaN
  • Trailing commas: not allowed in JSON, must be detected by checking that a comma is always followed by a value or closing bracket
  • Unterminated strings: tokenizer must ensure closing quote before end of input
  • Recursion depth: use explicit stack or depth counter to prevent stack overflow; discuss limits and error handling

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.