← Questions

Template formatter with plural blocks

0
FigmaCodingLast asked Jun 2024

A coding interview question asked in Figma's coding round, last asked Jun 2024. It tests strings, parsing and recursion.

Implement format_template(text: str, keys: list[str], values: list[str]) -> str. keys[i] maps to values[i]; all values are strings.

text can contain two kinds of expressions in braces:

  1. Placeholder {name}: replace it with the value of name. Whitespace around the name is ignored.
  2. Plural block {name, plural, one{...} other{...}}: the value of name must be an integer. If it is 1, use the one branch; otherwise use the other branch. If there is no one branch, use other. Inside the chosen branch, every # becomes the number. A branch may also contain placeholders, which are expanded too.

Rules:

  • Text outside expressions is copied unchanged. # outside a plural branch is a plain character.
  • An expression whose name is not in keys is left in the output unchanged.
  • Raise an error for an unmatched { or }, a block type other than plural, a plural block with no other branch, or a plural value that is not an integer.
  • 0 <= len(text) <= 10^4.

Example 1

text   = "{n, plural, one{# layer} other{# layers}} hidden"
keys   = ["n"]
values = ["4"]
→ "4 layers hidden"

With values = ["1"] the result is "1 layer hidden".

Example 2

text   = "{owner} shared {k, plural, one{# board with {team}} other{# boards with {team}}}"
keys   = ["owner", "k", "team"]
values = ["Rosa", "1", "Design"]
→ "Rosa shared 1 board with Design"

Clarifying questions

  • Is # replaced in a branch's literal text only, or also inside values substituted into that branch?
  • Are spaces allowed around plural, one and other, and between the branches?
  • Can plural blocks nest inside other plural branches?
  • Is a value like "01" or "-1" a valid integer, and does "01" select one?

What a strong answer covers

  • Writes a small recursive-descent parser with an index, instead of regular expressions, because branches contain braces of their own.
  • Distinguishes a placeholder from a plural block by the comma after the name, and finds each branch's matching } by tracking depth.
  • Replaces # in the chosen branch's text before expanding its placeholders, so a value containing # is not changed.
  • Leaves an expression with an unknown name exactly as written, by copying the original slice, and raises the listed errors with clear messages.
  • O(len(text)), tested on both examples, n = 0, a missing one branch, unbalanced braces, and a non-integer value.

Follow-ups

  • Add zero, two and exact-value branches such as =0{...}.
  • Add a select block that picks a branch by string value.
  • Support escaping braces and # with quotes.

Where it's asked

  • Figma · Coding · Last asked Jun 202460 min · One or two pair-programming rounds against provided tests

What it tests

strings · parsing · recursion

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding