Design a simple templating system for ad copy generation
Tests separation of concerns and API design. A good answer: data model separate from template, placeholder syntax, graceful missing-value handling, and HTML escaping. Red flag: naive string concatenation without validation or extensibility.
What's really being asked
This question evaluates whether you can draw a clean boundary between data and presentation. A template processor is software designed to combine templates with data to produce resulting documents or programs, so the interviewer wants to see you treat the ad copy generator as a small but complete template system rather than a one-off string concatenation hack.
The full answer
First, define a typed data model or schema that captures product name, price as a structured money object, and call to action, keeping the data layer separate from the template language. Second, choose a lightweight template syntax such as double curly braces and decide whether templates live as strings, files, or compiled functions. Third, describe a rendering pipeline with at least two stages, parsing the template into tokens and then merging those tokens with the data model to produce the formatted output. Fourth, handle missing variables explicitly through defaults, empty strings, or validation errors rather than silent undefineds. Fifth, flag security and formatting concerns, price should be locale formatted, not raw concatenation, and if the result document is a web page or HTML fragment, user supplied fields must be escaped to prevent injection.
The mistakes people make
Proposing a single function that takes three strings and returns a JavaScript template literal with no schema or validation. Hardcoding English word order so the system cannot support languages with different grammar structures. Treating price as a raw float string, which destroys currency formatting and invites rounding bugs. Ignoring that a template engine is ordinarily included as part of a larger system, so it should expose a clean API rather than global state.
What usually comes next
How would you add conditionals, for example showing a strikeout price only when a discount exists? How would you cache compiled templates when generating millions of ads? How would you support localization where word order changes across languages? What happens if a marketer accidentally passes HTML in a product name?
A concrete example
A minimal but solid design uses a JSON data object with fields productName, priceCents, currency, and ctaLabel. The template engine accepts a string like Buy {{productName}} for {{price}}. {{cta}}. The engine tokenizes the string, looks up each key in the data map, runs price through an i18n formatter, HTML-escapes productName, and returns the final string. Compiled templates are cached in a Map keyed by template hash so repeated renders are O of N on output length, not parsing cost.
Interview question
Which design choice best maintains separation of concerns in a simple ad copy templating system?
- a.Using a single function that interpolates product fields into a JavaScript template literal
- b.Formatting price as a raw float string inside the template concatenation step
- c.Hardcoding English grammar rules directly into the rendering logic
- d.Defining a typed data model and merging it with templates through a rendering pipelineCorrect
Why? this is the answer
A typed data model processed by an independent rendering pipeline keeps data and presentation cleanly separated. Option A is tempting because template literals resemble templating, but they tightly couple data access to presentation logic and lack schema validation or extensibility.
Just read this? Test yourself on what you have been reading.
Read the original → en.wikipedia.org
- #templating
- #system-design
- #api-design
- #security
- #localization
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles