Fill placeholders in a template string
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Amazon's coding round and at Figma, last asked May 2026. It tests strings, parsing and hash map.
Write a function that fills in a template string.
def format_template(template: str, keys: list[str], values: list[str]) -> str
template may contain placeholders written as {name}. The text between the braces is a variable name. keys[i] is a variable name and values[i] is the text that replaces it. Return the template with every placeholder replaced by its value. Copy all characters outside placeholders unchanged.
You may assume:
- every
{has a matching}, and placeholders are not nested; - every name used in
templateappears inkeys; -
keysandvalueshave the same length.
A replacement value is plain text. If a value itself contains something like {x}, it is not expanded again.
Constraints: 0 <= len(template) <= 10^5, 0 <= len(keys) <= 10^4.
Example 1
template = "Order {id} ships to {city} on {day}."
keys = ["city", "id", "day"]
values = ["Lisbon", "A-17", "Tuesday"]
→ "Order A-17 ships to Lisbon on Tuesday."
Example 2
template = "{greet}{greet}, {name}"
keys = ["greet", "name"]
values = ["hey", "{greet}"]
→ "heyhey, {greet}"
The value of name is copied as it is, so its braces stay in the output.
Clarifying questions
- If
keyscontains the same name twice, which value wins? - Can a name contain spaces or other characters between the braces?
- Should the function fail loudly if the assumptions break (a missing key or an unmatched brace)?
- Is there a way to write a literal brace in the template?
What a strong answer covers
- Builds a dictionary from
keystovaluesonce. - Scans the template once, copying text until
{, reading the name until}, and appending the value:O(len(template) + total output). - Collects pieces in a list and joins at the end, not repeated string concatenation.
- Never re-scans substituted values, which is why
{greet}in example 2 survives; explains why repeatedstr.replacecalls would get this wrong. - Tests both examples, an empty template, a template with no placeholders, and adjacent placeholders.
Follow-ups
- Support escaped braces such as
{{and}}. - Handle missing keys with a default value or an error.
- Allow nested templates that are expanded recursively, with cycle detection.
Where it's asked
- Amazon · Coding · Last asked May 202655 min · Usually two rounds; one is often an object-oriented design built in steps · No AI
- Figma
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.

