Template formatter with plural blocks
I was asked this
No reports yet. Sign in to say where you were asked it.
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:
-
Placeholder
{name}: replace it with the value ofname. Whitespace around the name is ignored. -
Plural block
{name, plural, one{...} other{...}}: the value ofnamemust be an integer. If it is1, use theonebranch; otherwise use theotherbranch. If there is noonebranch, useother. 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
keysis left in the output unchanged. - Raise an error for an unmatched
{or}, a block type other thanplural, a plural block with nootherbranch, 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,oneandother, 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"selectone?
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 missingonebranch, unbalanced braces, and a non-integer value.
Follow-ups
- Add
zero,twoand exact-value branches such as=0{...}. - Add a
selectblock 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
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
