Nested Set Structural Equivalence
Reported by candidates from Pinterest's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Strings up to 20000 characters, nesting up to 300 deep, and a Pinterest OA reported in July 2026 that asks if two encoded sets are equal. Brute force means trying every pairing of children at every level, and that blows up fast. The real move is canonicalization: parse each string into a tree, turn every set into a normalized form, then compare. If you've seen tree hashing or sorted serialization, this is that. If you blank mid-assessment, StealthCoder is the quiet backup running on your screen.
The problem
You are given two valid strings, left and right, that encode nested mathematical sets. An encoded set follows this grammar:
set := "{" [element ("," element)*] "}"
element := integer | set
An integer is a signed decimal integer. The encoding contains no whitespace. The empty set {} is valid.
Two encoded sets are equal when they contain the same distinct elements. The order of elements does not matter at any nesting level, and repeated equal elements at the same level collapse according to mathematical set semantics. Integer atoms compare by value, while nested sets compare recursively. An integer and a set are always different, so 1 is not equal to {1}.
Return true if left and right encode equal nested sets; otherwise, return false.
Function
nestedSetsEqual(left: String, right: String) → boolean
Examples
Example 1
left = "{1,{2,3},4}"
right = "{{3,2},4,1}"
return = true
Both top-level sets contain the integers 1 and 4 plus a nested set containing 2 and 3. Permuting either level does not change the set.
Example 2
left = "{1,{2,3}}"
right = "{{1,2},3}"
return = false
The nesting hierarchy differs. In left, 1 is a top-level atom and 3 is nested; in right, those roles are reversed.
Example 3
left = "{1,1,{},{{2},2}}"
right = "{{},1,{2,{2}}}"
return = true
The repeated top-level 1 collapses. Both sides then contain 1, the empty set, and a nested set whose elements are the atom 2 and the set {2}.
Constraints
2 <= left.length, right.length <= 20000
Each input is a valid encoding generated by the stated grammar and contains no whitespace.
Every integer is in [-10^9, 10^9] and uses ordinary decimal notation.
The maximum nesting depth is at most 300.
Repeated recursively equal elements at one set level represent one mathematical set element.Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to give every set a canonical form that ignores order and duplicates. Parse recursively with an index pointer. For each set, compute canonical strings for its children: integers stay as normalized numbers, nested sets return their own canonical string. Then dedupe the children, sort them, and join with commas inside braces. Two sets are equal exactly when their canonical strings match. Depth is capped at 300, so recursion is fine, but an iterative stack is safer in some languages. The common pitfall is forgetting duplicates, like the repeated 1 in Example 3. Another is treating 1 and {1} as the same, so keep atoms and sets distinguishable. Also watch negative numbers while parsing. Sorting repeated canonical strings at each level can cost extra, so an alternative is to map each canonical form to an integer ID with a hash map. If the parser trips you up live, StealthCoder is your hedge during the OA.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Nested Set Structural Equivalence cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Pinterest's OA.
Pinterest reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Nested Set Structural Equivalence FAQ
What's the trick in the Pinterest nested set equality problem?+
Canonicalize. Parse each string recursively, convert every child to a canonical form, dedupe, sort, and rebuild the string. Equal sets produce identical canonical strings, so the final answer is a single string comparison.
How do I handle duplicates like {1,1,{}}?+
Dedupe the children at each level after canonicalizing them. Put the canonical child strings into a set, or sort and skip adjacent repeats. Do this at every nesting level, not just the top, since the rules apply recursively.
Why not just sort the raw strings?+
Sorting the raw text doesn't work because nested sets can be written in different orders internally. {2,3} and {3,2} look different as text. You have to normalize inner sets first, bottom-up, then sort the results at the parent level.
Will recursion blow the stack with 20000 characters?+
Max depth is 300, so normal recursion is fine in most languages. The length is large, but depth is small. Use an index pointer instead of slicing substrings so parsing stays linear and you avoid copying.
How do I prep for this in 48 hours?+
Write a recursive descent parser for the grammar, including negative integers. Then add canonical sorting and dedupe. Test on the three examples, plus {} versus {{}} and 1 versus {1}. Those edge cases are where most wrong answers come from.