Rich Text Parser Part 3 (Depth Tokens)
Updated
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.
Part 3. This was the final twist of the real interview. After the Part 2 parser passed, the interviewer swapped the tokenizer for a different implementation and asked for the same result. The new tokenizer returns a flat list in which every word is preceded by its nesting depth: tokenize("(a,(b),c)") now returns [1, 'a', 2, 'b', 1, 'c']. There are no bracket tokens anymore.
The final question adds one more requirement: the parser must also accept a stream with no nesting, in which top-level words carry depth 0.
Implement readRichText(tokens) that produces the same serialized nested structure that Part 2 produced, from this new token format. On FastPrep the flat list arrives as string tokens: depths appear as strings such as "1", each followed by its word.
Rules of the depth encoding.
- depths are non-negative integers. Depth
0means the word sits directly in the document, outside every group, so a stream whose depths are all0has no nesting at all. The first depth may be greater than1 - between consecutive words the depth may rise or fall by more than
1: each unit of rise opens one new group, each unit of fall closes one - consecutive words at the same depth are siblings in the same group, and a word that returns to a still-open depth joins that open group
- falling to depth
0closes every open group, so a later positive depth opens a new top-level group instead of reopening an earlier one - this encoding cannot represent empty groups, so none appear
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']], while a stream with no nesting returns a flat list such as['a', 'b']
In Python, str(nested_list) produces exactly this format.
Function
readRichText(tokens: String[]) → StringExamples
Example 1
tokens = ["1", "a", "1", "b", "1", "c"]return = "[['a', 'b', 'c']]"All words have depth 1, so they belong to the same top-level group: ['a', 'b', 'c']. The full parsed document is [['a', 'b', 'c']].
Example 2
tokens = ["1", "a", "2", "b", "1", "c"]return = "[['a', ['b'], 'c']]"This is tokenize("(a,(b),c)") from Part 3. The word b has depth 2, so it becomes a nested group between a and c.
Example 3
tokens = ["3", "a", "2", "b", "1", "c"]return = "[[[['a'], 'b'], 'c']]"This is read_rich_text(tokenize("(((a)b)c)")) from Part 2. The first depth is 3, so a sits inside three groups; the depths then fall to 2 and 1, closing one level each time. Together with the outer document list the answer has four leading brackets.
Example 4
tokens = ["1", "a", "3", "b"]return = "[['a', [['b']]]]"Only words carry depth labels, so the depth can jump from 1 straight to 3: two new groups open at once and b lands inside both.
Example 5
tokens = ["0", "a", "0", "b"]return = "['a', 'b']"Both words have depth 0, so neither sits inside a group: the stream has no nesting and the document is the flat list ['a', 'b'].
Example 6
tokens = ["0", "a", "1", "b", "2", "c", "0", "d", "1", "e"]return = "['a', ['b', ['c']], 'd', ['e']]"a is a top-level word. The rise to 1 opens a group for b, and the rise to 2 opens a group inside it for c. Falling to 0 closes both groups, so d is top-level again, and the final rise to 1 opens a new group for e rather than reopening the first one.
Constraints
0 <= tokens.length <= 5000, always an even number: alternating depth and word- depths are decimal strings between
0and50 - words are lowercase alphanumeric with at most 10 characters
More Figma Onsite Interview questions
- Recursive Display SortPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Rich Text Parser Part 2 (Parse Nested Groups)PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Rich Text Parser Part 1 (Validate Tokens)PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Table Selection and Color UpdatesPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Sort Documents Left to RightONSITE INTERVIEW · Seen Aug 2026
- Symmetric Binary TreeONSITE INTERVIEW · Seen Feb 2026
- Topmost Accessible NodesPHONE SCREEN · ONSITE INTERVIEW · Seen Jan 2025
- Nested Template Variable ExpansionONSITE INTERVIEW · Seen Jun 2024
- Parse, Validate, and Repair Markdown FormattingONSITE INTERVIEW · Seen Jun 2024
- Breadth-First Values of a Binary TreeONSITE INTERVIEW · Seen Oct 2022
Similar questions at other companies
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Remove Parentheses from a Plus-Minus ExpressionOracle · PHONE SCREENOracle interview questions
- Decode StringCompass International Holdings · PHONE SCREENCompass International Holdings interview questions
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Molecular Weight of a Grouped FormulaAgoda · ONSITE INTERVIEWAgoda interview questions
- Calculate a Token-List ExpressionCollege Vine · ONSITE INTERVIEW
- Parse Nested HTML into a TreeApple · ONSITE INTERVIEWApple interview questions
- Calculator with ParenthesesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Lisp Expression ParserAmazon · ONSITE INTERVIEW