KatInterview

Resolve Variables from Linked Assignment Rules with Cycle Checks

Trees & GraphsModel answer
MediumModel answerTrees & Graphsparsingmemoizationdfs

Last updated

Evaluate a target variable from a list of assignment strings, growing from simple aliasing to signed sums of variables and integers, then detect circular dependencies safely.

You are given a list of assignment strings and the name of a target variable. Compute the integer value of the target.

Example: with target T1 and the assignments

T1 = T2
T2 = 5

the result is 5, because T1 refers to T2, which holds 5.

The task is built up in three stages. Design your solution so that each stage extends the previous one.

Stage 1: Alias chains

The right-hand side of every assignment is either an integer literal or the name of exactly one other variable. Return the target's value by following the chain of references.

Discuss and handle:

  • How you store the assignments for fast lookup.
  • How you avoid resolving the same variable repeatedly.
  • What happens when the target or a variable it relies on is never assigned.
  • What happens when the same variable is assigned more than once.

Stage 2: Sums and differences

The right-hand side may now be an expression made from variable names and integers joined by + and -.

T1 = T2 + 3 - T3
T2 = 5
T3 = T2 - 1

Here T3 = 4, so T1 = 5 + 3 - 4 = 4.

Requirements:

  • Whitespace is optional, so T2+3 is valid.
  • Malformed expressions such as T2 3 or T2 + must be rejected with a clear error.
  • Decide and state your rule for a leading sign (T1 = -T2) and for consecutive operators.
  • A variable used by several other variables must not be recomputed each time.

State the time complexity in terms of the number of variables and the total length of all expressions.

Stage 3: Circular definitions

The input may now contain cycles:

A = B + 1
B = C
C = A - 2

Instead of recursing forever, detect the cycle and report an error.

Discuss and handle:

  • Telling a genuine cycle apart from a variable that is merely reached twice through different routes (a shared dependency is not a cycle).
  • Whether you reject the whole input or only fail when the target actually depends on the cycle.
  • Whether the error names the variables on the cycle.
  • Behavior on very long chains, where deep recursion could overflow the stack.

Provide the code, its complexity, and the test cases you would run for each stage.

Hints

Hint 1

Parse every line once into a dictionary from variable name to a list of signed terms (each term is either an integer or a variable name with a sign). Stage 1 is just the special case of a single positive term.

Hint 2

Use three states per variable during DFS: unvisited, in progress, done. Reaching an in-progress variable means a real cycle; reaching a done variable simply reuses its cached value, which is why diamonds are not flagged.

Hint 3

To report the cycle and avoid recursion limits, keep an explicit path stack. When you hit an in-progress variable, slice the path from its first occurrence to get the cycle members. An iterative DFS with a stack of (variable, term index) frames removes the depth limit.