Rich Text Parser Part 1 (Validate Tokens)
Problem statement
π Hi there! The description you are currently reading is one part of a 3-part Figma interview set. It is highly recommended to read ALL THE PARTS before coding as parts build on top of each other π³
- Rich Text Parser Part 1 (Validate Tokens) π¦
- Rich Text Parser Part 2 (Parse Nested Groups) πΉ
- Rich Text Parser Part 3 (Depth Tokens) π¦
At Figma, documents are stored as nested structures: a paragraph can contain text runs and nested groups, which can themselves contain more groups. You are working with a small text format that describes structures like this. This set reconstructs a real ~45-minute Figma coding screen, part by part.
Setup. The interview starts from a tokenizer that is already written for you. For example, tokenize("(a,b,c)") returns ["(", "a", "b", "c", ")"]. Notice that commas are separators: the tokenizer drops them, so every token you receive is "(", ")", or a word. On FastPrep the tokenizer's output is handed to you directly as the tokens array.
Part 1. Before parsing anything, the interviewer asks for a validator. Implement isWellFormed(tokens) that returns true when the bracket structure of the token stream is well-formed: scanning left to right, no ")" ever appears without a matching earlier "(", and every "(" is eventually closed.
Words never affect validity and may appear anywhere, including outside all groups. A stream with no tokens is well-formed. Multiple separate groups at the top level are allowed.
Function
isWellFormed(tokens: String[]) β booleanExamples
Example 1
tokens = ["(", "a", "(", "b", ")", "c", ")"]return = trueThis is tokenize("(a,(b),c)"). The inner group closes before the outer one; every open has a matching close.
Example 2
tokens = ["(", "a", "b", "c"]return = falseThis is tokenize("(a,b,c"). The group is never closed.
Example 3
tokens = ["(", "a", "(", ")", ")", "(", "b", "c", ")", ")"]return = falseThis is tokenize("(a,())(b,c))"). Everything up to (b,c) is balanced, but the final ")" has no matching open.
Constraints
0 <= tokens.length <= 5000- each token is
"(",")", or a lowercase alphanumeric word with at most 10 characters