KatInterview

Variable Registry With Deep Reference Expansion and Cycle Detection

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designstringsrecursionhash-map

Last updated

Tests recursive variable substitution with memoization, cycle detection across direct and indirect references, and clean handling of undefined variable references.

Variable Registry With Deep Reference Expansion and Cycle Detection

You are building a lightweight variable registry that stores named values and resolves references embedded in text.

Implement a simulation of this registry by processing a sequence of operations. Each operation is one of the following:

  • "set" — Store a variable under the given name with the given value. If the name already exists, overwrite it. Returns None.
  • "get" — Return the raw stored value (unexpanded) for the given name, or None if the name is not present.
  • "remove" — Delete the variable with the given name if it exists. Returns None.
  • "substitute" — Resolve all variable references in the given text and return the resulting string (see expansion rules below).

Reference Syntax

Inside any text (including stored values), a reference to a variable named foo is written as ${foo}.

Expansion Rules

When substitute is called on a text:

  1. Scan the text from left to right for patterns of the form ${name}.
  2. When a reference is found, look up name in the registry.
    • If name is not in the registry, leave the reference exactly as written (e.g. ${missing} stays ${missing}).
    • If name exists, replace the reference with that variable's value and recursively expand any references inside that value. There is no depth limit.
  3. Values are expanded on demand; the registry stores raw values.

Cycle Detection

If expanding a reference would require expanding a variable that is already being expanded in the current call chain, a cycle exists. In that case, substitute must return a dictionary:

{"error": "cycle: v1 -> v2 -> ... -> v1"}

The cycle path lists each variable in the order they were first encountered, then repeats the starting variable at the end. The cycle detection must not trigger when a variable is simply referenced from more than one place (shared dependencies are fine).

If no cycle is present and no other error occurs, substitute returns the fully expanded string.

Function Interface

def variable_registry(operations: List[str], arguments: List[List[str]]) -> List[object]:
  • operations: a list of operation names as described above.
  • arguments: parallel list of argument lists. Each entry contains the arguments for the corresponding operation.
    • "set": [name, value]
    • "get": [name]
    • "remove": [name]
    • "substitute": [text]
  • Returns a list with one entry per operation, matching the return values described above.

Constraints

  • 1 <= len(operations) <= 1000
  • Variable names contain only letters (a–z, A–Z), digits (0–9), and underscores (_). They do not start with a digit.
  • Stored values and substitute inputs are non-empty strings of printable ASCII characters.
  • Stored values may themselves contain ${name} references.
  • The registry state persists across operations within the same call (i.e. a set affects all later operations).
  • A reference with no closing } is treated as a literal and left unchanged.
  • References do not nest inside the name portion (no ${user_${env}}).

Examples

Example 1

Input: variable_registry(['set', 'set', 'set', 'substitute', 'substitute', 'get'], [['host', 'db.internal'], ['port', '5432'], ['url', '${host}:${port}/app'], ['connect to ${url}'], ['${missing}'], ['host']])

Output: [None, None, None, 'connect to db.internal:5432/app', '${missing}', 'db.internal']

Example 2

Input: variable_registry(['set', 'set', 'substitute'], [['a', 'x${b}'], ['b', '${a}'], ['${a}']])

Output: [None, None, {'error': 'cycle: a -> b -> a'}]

Hints

Hint 1

Store raw values in a plain dictionary. Expand references only when substitute is called, not when set is called — that way updates stay simple and the expansion always reflects the current registry state.

Hint 2

For expand(text, path): scan left to right for ${ ... } patterns. When you find a reference, check if the variable name already appears in path (cycle!) before recursing into its value. Append the name to path for the recursive call.

Hint 3

A variable can be referenced from multiple places without forming a cycle — being referenced twice is not the same as being in the current expansion chain. Use the call-stack path list, not a global visited set, to tell the difference.