Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Before a raw event payload is handed to the JSON parser, an ingestion service runs a cheap structural check, so a truncated or corrupted message can be quarantined with a clear reason instead of a parser stack trace. The check is about brackets: every {, [ and ( must be closed by its matching character, in the right order.
Write brackets_balanced(payload). It takes one payload as a string of JSON-like text (possibly empty) and returns True when its brackets are balanced and properly nested, False otherwise.
Function to write
brackets_balanced(payload: str) -> boolTrue when every bracket outside a quoted string is closed by its match in the right order.
How to approach it
Push each opener and make each closer pop the newest one and match it. Decide which characters are inside a string before you look at them as brackets at all.
Sample cases
+ 3 held back until you submit
a bracket inside a quoted note
The ) sits inside a string, so it is text; the real brackets balance and the answer is True.
Input
Argument 1
'{"note": "use ) carefully", "tags": ["a", "b"]}'Returns
Truecrossed brackets
Two opens and two closes, but } arrives while [ is still open, so the nesting is wrong.
Input
Argument 1
'{"rows": [1, 2}]'Returns
Falsean empty payload
Nothing is opened, so nothing is left unclosed and the empty payload is balanced.
Input
Argument 1
''Returns
Truenested objects inside a list
Four levels of nesting, each closed in the right order, so the payload is balanced.
Input
Argument 1
'{"batch": [{"id": 1, "tags": ["x"]}, {"id": 2}]}'Returns
TrueConstraints
(), [] and {}. Every closer must match the most recently opened bracket that is still open, so {"rows": [1, 2}] is False because } arrives while [ is still open.{ or [ makes the answer False. A closer with nothing open is also False.{"note": "use ) carefully"} is balanced.\" does not end the string. Outside a string a backslash is an ordinary character.True: nothing was opened, so nothing is left unclosed. Always return a bool.Worked example
Take {"note": "use ) carefully", "tags": ["a", "b"]}. The { is outside any string, so push it: the stack is {. A quote opens note and the next quote closes it. Another quote opens the note text, and the ) inside it is text, so it is skipped; the next quote closes the string. [ is pushed, so the stack is { [. "a" and "b" are strings. ] matches the top [, so pop to {; } matches {, so pop to empty. The scan ends with an empty stack, so the answer is True.
The trap is a counter. On {"rows": [1, 2}] a depth counter sees two opens and two closes and says True, because it never checks which bracket a closer closes; and without string handling the ) in the note is counted as a close, so the final } drives the count below zero and a valid payload is rejected.
What this tests
The stack pattern: LeetCode 'Valid Parentheses' (problem 20) in data clothing, plus the string state a real tokenizer needs. O(n) time in one pass and O(d) space for nesting depth d. A single counter is O(1) space but wrong, because the stack is the smallest structure that remembers order. 'Valid Parentheses' (also asked as 'Balanced Parentheses') is reported by an Atlassian data engineering candidate and tagged for data engineers at Meta and others on Exponent. A data engineer meets it when triaging malformed JSON in a dead-letter queue, pre-checking a template before submission, or writing a stream splitter that must know where one object ends.
brackets_balanced(payload: str) -> boolSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.