FastPrepRich Text Parser Part 2 (Parse Nested Groups)

Rich Text Parser Part 2 (Parse Nested Groups)

Figma logoFigma● MediumFULLTIMEPHONE SCREENONSITE INTERVIEW
Learn

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 🐳

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[]) β†’ String

Examples

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

More Figma problems

See Figma hiring insights
public String readRichText(String[] tokens) {
  // write your code here
}
tokens["(", "a", "b", "c", ")"]
expected"[['a', 'b', 'c']]"
Checking account…