Problem, as an interviewer would state it:
"Write a template compiler supporting {{variable}} and {{object.path}} interpolation — compile(template) should return a fast, reusable render function, doing the expensive PARSING work only once."
Examples:
const render = compile("Hello {{name}}, you are {{user.age}} years old.");
render({ name: "Ada", user: { age: 30 } }); // "Hello Ada, you are 30 years old."
render({ name: "Grace", user: { age: 45 } }); // reuses the same compiled parts
Clarifying questions expected:
- Should parsing genuinely happen only ONCE per template, with the returned function reused efficiently across many renders?
- What should a missing or undefined interpolation path render as — an empty string, or the literal word "undefined"?
- Does the interpolation syntax need to support nested object paths, not just top-level variable names?
Code / implementation expected: Yes — real, direct proof that compiling happens exactly once (via a real counter) across 3 separate renders with different data, and that a missing path renders as an empty string.