← Questions

Fill placeholders in a template string

0
FigmaAmazonCodingLast asked May 2026

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 template appears in keys;
  • keys and values have 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 keys contains 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 keys to values once.
  • 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 repeated str.replace calls 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

strings · parsing · hash map

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