Resolve Variable Assignments With Addition and Subtraction, Flagging Unresolvable Ones
Company: Applied Intuition
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
You are given a list of assignment lines. Each line assigns to a variable either an integer or an expression that adds and subtracts integers and other variables. Determine the value of every variable that can be resolved, and identify every variable that cannot.
The report gives the input of Example 1 without its expected output, and does not say how a variable assigned twice, a variable that is never assigned, or a cycle is treated; this version fixes those rules in Rules below.
### Function Signature
```python
def resolve_variables(lines: list[str]) -> list[str]:
```
### Rules
- Every line has the form `name = term`, or `name = term op term ... op term`, with single spaces between tokens. Each `op` is `+` or `-`, and each term is a variable name or a non-negative integer literal.
- The lines are definitions, not statements executed in order: a line may use a variable that is defined on a later line.
- If several lines assign the same variable, only the last of those lines defines it, and the earlier ones are ignored. For example, with the line `x = 5` followed later by `x = x + 1`, the definition of x is `x = x + 1`, so x refers to itself and cannot be resolved.
- A variable is resolved when it has a definition and every variable in that definition is resolved. Its value is the definition evaluated from left to right with exact integer arithmetic.
- A variable cannot be resolved if it has no definition (it appears only on right-hand sides), if its definition depends on itself directly or through a cycle of definitions, or if its definition uses a variable that cannot be resolved.
- Return one string for each distinct variable name that appears anywhere in `lines`, on either side of `=`, sorted by name in ascending lexicographic order. Each string has the form `name = value`, where value is the resolved value in decimal with a leading `-` when negative, or the word `UNRESOLVED` when the variable cannot be resolved.
### Constraints
- `1 <= len(lines) <= 10^4`
- Every right-hand side has between 1 and 50 terms, and all lines together have at most `10^5` terms.
- A variable name is 1 to 10 lowercase English letters.
- An integer literal is a decimal number from 0 to `10^9`, written without a sign and without leading zeros.
- For every variable that can be resolved, its value and every left-to-right partial result while evaluating its definition lie within `[-10^15, 10^15]`.
- Chains of definitions can be as long as the number of lines.
- Every line is well formed.
### Examples
**Example 1**
```text
Input: lines = ["a = b + c", "d = d + 5", "c = 5", "d = 10"]
Output: ["a = UNRESOLVED", "b = UNRESOLVED", "c = 5", "d = 10"]
```
The later line `d = 10` replaces `d = d + 5`, so d is 10, and c is 5. b is never assigned, so b cannot be resolved, and neither can a, which uses it.
**Example 2**
```text
Input: lines = ["x = y - 3", "y = 10 + z", "z = 2", "w = x - y + 1"]
Output: ["w = -2", "x = 9", "y = 12", "z = 2"]
```
z is 2, so y = 10 + 2 = 12 and x = 12 - 3 = 9, even though x's line comes before y's. Then w = 9 - 12 + 1 = -2.
**Example 3**
```text
Input: lines = ["p = q + 1", "q = p - 1", "r = 4", "s = r + p", "t = 7 - 7"]
Output: ["p = UNRESOLVED", "q = UNRESOLVED", "r = 4", "s = UNRESOLVED", "t = 0"]
```
p and q depend on each other, so neither can be resolved, and s uses p. t uses no variables and equals 0.
Overview: A coding task that evaluates a list of variable assignments built from integers, other variables, addition and subtraction, where definitions may appear in any order. It asks for every resolvable value and flags variables that are undefined, cyclic or dependent on unresolvable ones, testing dependency resolution and cycle handling.
Read the full Applied Intuition Software Engineer interview experience this question came from