Reported June 2024
Figmamemoization

Nested Template Variable Expansion

Reported by candidates from Figma's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Figma OA. Under 2s to a working solution.
Founder's read

Figma flagged this one in June 2024, and the detail that matters is the {name|singular|plural} placeholder. It looks like string replacement, but it's recursive expansion with a pluralization rule layered on top. You're rendering a template where variable values can reference other variables, up to 1000 levels deep, with 200000 characters of input. If you're taking this OA soon, the pattern is memoized recursion over a parsed string. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the plan below should get you most of the way.

The problem

Render a text template using named variables. Each row of variables is [name, value].
The template and variable values may contain two placeholder forms:
{name} inserts the fully expanded value of that variable.
{name|singular|plural} requires the fully expanded value of name to be a non-negative integer. It inserts the number, one space, and singular when the number is 1; otherwise it inserts the number, one space, and plural.
Variable values may reference other variables. Expand them recursively, then render the input template.

Function
renderTemplate(text: String, variables: String[][]) → String

Examples
Example 1
text = "I like {animal}."
variables = [["animal","dogs"]]
return = "I like dogs."
The simple placeholder is replaced by the value of animal.
Example 2
text = "Adopt {count|dog|dogs}."
variables = [["count","1"]]
return = "Adopt 1 dog."
The numeric value is exactly 1, so the singular form is selected.
Example 3
text = "{greeting} You have {count|message|messages}."
variables = [["name","Mina"],["greeting","Hello, {name}!"],["rawCount","2"],["count","{rawCount}"]]
return = "Hello, Mina! You have 2 messages."
Both greeting and count expand through another variable before the outer template is rendered.

Constraints
1 <= template.length <= 200000.
1 <= variables.length <= 100000.
Variable names are unique and contain only ASCII letters, digits, and underscores.
Every referenced variable exists, references are acyclic, and nesting depth is at most 1000.
Singular and plural labels are non-empty and contain no braces or |.
The rendered output length is at most 10^6.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to expand each variable once and cache it. Build a map from name to raw value, then write expand(text) that scans for braces. For {name}, call a memoized getValue(name), which expands that variable's raw value through the same function. For {name|a|b}, split on the pipe, expand the name, parse it as an integer, and pick singular only when it equals 1. The pitfall is skipping the cache. Shared variables get re-expanded and you blow the time budget. The second pitfall is recursion depth. Nesting reaches 1000, which can overflow the stack in some languages, so use an explicit stack or raise the limit. Build output with a list and join it, not repeated concatenation. If the parsing logic falls apart under pressure, StealthCoder can give you a working solution in the live OA, but know the memoized shape before you sit down.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Nested Template Variable Expansion cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Figma's OA.

Figma reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Nested Template Variable Expansion FAQ

What's the core trick in the Figma template expansion problem?+

Memoized recursive expansion. Store each variable's fully expanded value after the first computation, so a variable referenced many times is only expanded once. Then render the main text with the same scanner. Without the cache, deep or repeated references get slow fast.

How hard is this really?+

Medium, mostly because of parsing, not algorithms. The logic is simple once you see recursion plus a cache. The bugs come from brace handling, splitting on the pipe, and the exact singular rule where only the number 1 picks singular, including 0 using plural.

Do I need to worry about recursion depth?+

Yes. Nesting depth can reach 1000, and the template is up to 200000 characters. In Python, raise the recursion limit or go iterative. In Java, a deep call chain can overflow the default stack. An explicit stack or running the work in a bigger-stack thread avoids the risk.

How should I build the output string efficiently?+

Append pieces to a list or string builder and join once at the end. Repeated concatenation in a loop can turn quadratic on outputs up to 10^6 characters. Cache expanded variable strings so you reuse them instead of rebuilding them.

How do I prepare for this in 48 hours?+

Write a small brace scanner by hand and test it on the three examples, especially the chained count to rawCount case. Then add memoization and test a long chain of variables. Practice parsing and splitting on the pipe, since that's where most mistakes show up.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Figma.

OA at Figma?
Invisible during screen share
Get it