PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Microsoft

Validate a JSON-like string

Last updated: Mar 29, 2026

Quick Overview

This question evaluates parsing and string-processing skills, focusing on structural validation of JSON-like text including matching and nesting of brackets and correct handling of quoted strings with escape sequences.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Validate a JSON-like string

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem You are given a string `s` that is intended to be a JSON text. Implement a function `isValidJsonStructure(s) -> bool` that checks **structural validity** of the JSON with the following simplified rules: ### Rules to validate 1. After ignoring leading/trailing whitespace, the text must start and end with matching outer brackets: - either `{ ... }` or `[ ... ]`. 2. Brackets must be **properly nested and balanced**: - curly braces `{}` - square brackets `[]` 3. JSON strings are delimited by **double quotes** `"..."`. - While inside a string, any bracket characters (`{ } [ ]`) must be ignored for nesting purposes. - A quote preceded by an **odd** number of backslashes is escaped (e.g., `\"` closes? no; `\"` contains an escaped quote). In other words, `"` does **not** end a string. 4. You do **not** need to validate JSON key/value grammar (colons, commas, numbers, `true/false/null`, etc.). Only validate the above structural properties. ### Input/Output - **Input:** string `s` - **Output:** `true` if structurally valid per the rules; otherwise `false`. ### Constraints - `1 <= len(s) <= 1e5` ### Examples - `"{\"a\":[1,2,3]}"` → `true` - `"{[}]"` → `false` (bad nesting) - `"{\"x\":\"[}\"}"` → `true` (brackets inside a string are ignored) - `"{\"a\":\"unterminated}"` → `false` (unclosed string)

Quick Answer: This question evaluates parsing and string-processing skills, focusing on structural validation of JSON-like text including matching and nesting of brackets and correct handling of quoted strings with escape sequences.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
Microsoft logo
Microsoft
Jan 6, 2026, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
9
0
Loading...

Problem

You are given a string s that is intended to be a JSON text. Implement a function isValidJsonStructure(s) -> bool that checks structural validity of the JSON with the following simplified rules:

Rules to validate

  1. After ignoring leading/trailing whitespace, the text must start and end with matching outer brackets:
    • either { ... } or [ ... ] .
  2. Brackets must be properly nested and balanced :
    • curly braces {}
    • square brackets []
  3. JSON strings are delimited by double quotes "..." .
    • While inside a string, any bracket characters ( { } [ ] ) must be ignored for nesting purposes.
    • A quote preceded by an odd number of backslashes is escaped (e.g., \" closes? no; \" contains an escaped quote). In other words, " does not end a string.
  4. You do not need to validate JSON key/value grammar (colons, commas, numbers, true/false/null , etc.). Only validate the above structural properties.

Input/Output

  • Input: string s
  • Output: true if structurally valid per the rules; otherwise false .

Constraints

  • 1 <= len(s) <= 1e5

Examples

  • "{\"a\":[1,2,3]}" → true
  • "{[}]" → false (bad nesting)
  • "{\"x\":\"[}\"}" → true (brackets inside a string are ignored)
  • "{\"a\":\"unterminated}" → false (unclosed string)

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.