KatInterview

Resolve Nested %NAME% Placeholders with Cycle Detection

StringsAuto-graded · 27 test cases
MediumAuto-gradedStringsparsingdfsmemoization

Last updated

Expand %NAME% placeholders whose values may reference other placeholders, handling %% escapes, missing names, and circular references.

Render a Template with Nested Placeholders

You are given a text template and a dictionary definitions that maps names to replacement text. Produce the fully rendered text by substituting every placeholder in template.

Token rules

Scan a string from left to right. At each % character, apply the first rule that fits:

  1. Escape: if the next character is also %, the pair %% produces one literal %. Both characters are consumed.
  2. Placeholder: otherwise, if the % is followed by one or more characters from [A-Za-z0-9_] and then another %, the whole %NAME% span is a placeholder for the name NAME. All of the name characters and the closing % are consumed.
  3. Stray percent: in any other case, the % is ordinary literal text, and scanning continues with the very next character.

Examples of how text is split:

TextMeaning
100%% doneliteral text 100% done
50% offliteral text 50% off
%%USER%literal %, then literal USER%
%A%B%placeholder A, then literal B%
%A %B%literal %A , then placeholder B

Substitution

  • A placeholder %NAME% is replaced by the fully rendered form of definitions["NAME"].
  • Each definition is itself a string that follows the same token rules, so it may contain %% escapes and further placeholders, which are rendered recursively.
  • Every string (the template and each definition) is tokenized exactly once from its own raw text. Text produced by a substitution is final and must never be scanned again. For example, a rendered % that came from %% can never combine with neighboring characters into a new placeholder.

Errors

  • If a placeholder refers to a name that is not a key of definitions, raise KeyError.
  • If rendering a name requires, directly or indirectly, rendering that same name again (including a name whose definition refers to itself), raise ValueError.
  • Only definitions that are actually reached while rendering are examined. An unused definition that is broken (missing reference or cycle) must not cause an error.
  • If several problems exist, the one met first when processing left to right, depth first, is the one raised.

Return the rendered string.

Constraints

  • 0 <= len(template) <= 10^4
  • 0 <= len(definitions) <= 1000
  • Every key of definitions matches [A-Za-z0-9_]+
  • All strings contain printable ASCII characters only
  • Reference chains may be as long as the number of definitions, so very deep nesting must work

Examples

Example 1

Input: render_template('I am %USER%. My home is %HOME%.', {'USER': 'admin', 'HOME': '/%USER%/home'})

Output: 'I am admin. My home is /admin/home.'

Example 2

Input: render_template('Usage: 100%% completed by %USER%', {'USER': 'alice'})

Output: 'Usage: 100% completed by alice'

Hints

Hint 1

Separate the work into two steps: splitting a raw string into literal pieces and placeholder names, then resolving names. All the % rules belong only in the first step.

Hint 2

Treat each name as a node in a graph and resolve it with a depth-first search. Cache finished names so each is rendered once, and keep a set of names currently being resolved to spot a cycle.

Hint 3

A chain of 1000 names can exceed Python's default recursion limit, so use an explicit stack of frames (name, tokens, next index, output pieces). Never re-tokenize text that has already been rendered.