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.
Return whether a JSON-like string has matching outer brackets and balanced nested brackets while ignoring bracket characters inside strings.
Constraints
- Only structural bracket validity is checked.
- Quotes preceded by an odd number of backslashes are escaped.
- JSON key/value grammar is not validated.
Examples
Input: ('{"a":[1,2,3]}',)
Expected Output: True
Explanation: Balanced brackets outside strings are valid.
Input: ('{[}]',)
Expected Output: False
Explanation: Bad nesting is invalid.
Input: ('{"x":"[not a bracket]"}',)
Expected Output: True
Explanation: Brackets inside strings are ignored.
Input: ('{"x":"a\\"b"}',)
Expected Output: True
Explanation: Escaped quotes do not end the string.
Input: ('abc',)
Expected Output: False
Explanation: The text must have matching outer brackets.
Hints
- Track strings separately from brackets.
- Use a stack for bracket nesting.