Rich Text Parser Part 2 (Parse Nested Groups)
Learn this problemProblem statement
π Source note, July 7, 2026: This set reconstructs a real Figma MLE full-time coding screen, shared first-hand by a candidate. For this part, all four expected outputs are the interviewer's own asserts, character for character, including the double-wrapped [['a', 'b', 'c']] shape, so what's graded is a 100% match. The surrounding wording and constraints are FastPrep's reconstruction. As a whole we'd call this page a 95% match. If you've met this question in an interview and remember it differently, tell us and we'll tighten it. Thank you! π
π 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 π³
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 2. Your validator from Part 1 passed, so the interviewer moves on: implement readRichText(tokens) that parses the token stream into a nested list structure. Every ( β¦ ) group becomes a nested list, every word becomes a string, and items that appear next to each other at the same level are siblings in the same enclosing container.
The input is guaranteed well-formed β checking that was Part 1's job.
Output format. Return the parsed document as a Python-style list string:
- a group serializes as
[β¦]with children joined by a comma and a single space - words are wrapped in single quotes
- the return value is the serialization of the document: the list of all top-level items. A document that consists of one group therefore starts with two brackets, e.g.
[['a', 'b', 'c']]
In Python, str(nested_list) produces exactly this format.
Function
readRichText(tokens: String[]) β StringExamples
Example 1
tokens = ["(", "a", "b", "c", ")"]return = "[['a', 'b', 'c']]"This is tokenize("(a,b,c)"). One group with three words. The document is the list of top-level items, so the single group is wrapped once more: [['a', 'b', 'c']].
Example 2
tokens = ["(", "a", "(", "b", ")", "c", ")"]return = "[['a', ['b'], 'c']]"This is tokenize("(a,(b),c)"). The inner group becomes a nested list between a and c.
Example 3
tokens = ["(", "a", "b", "c", "(", ")", ")"]return = "[['a', 'b', 'c', []]]"This is tokenize("(a,b,c())"). c and the empty group are two separate siblings β an empty group serializes as [].
Example 4
tokens = ["(", "(", "(", "a", ")", "b", ")", "c", ")"]return = "[[[['a'], 'b'], 'c']]"This is tokenize("(((a)b)c)"). A word that follows a closed group is a sibling of that group: b sits beside ['a'], and c beside [['a'], 'b'].
Constraints
0 <= tokens.length <= 5000- each token is
"(",")", or a lowercase alphanumeric word with at most 10 characters - the token stream is well-formed and nesting depth is at most 50