Resolve Variable Equations with Dependency Errors
Updated
Problem statement
You are given an array of variable definitions. Each definition looks like name = term + term - term + ..., with a plus or a minus sign between two terms. A term is a signed integer or the name of another variable. Names use only lowercase English letters. There may be spaces around names, integers, = and the operators. There are no parentheses.
Work out every variable. Return one string per definition, in the same order as the input. Each string looks like name=value. Calculate from left to right. A minus sign right before an integer belongs to that integer, so foo - -3 subtracts negative three.
Some inputs are broken. If the variables depend on each other in a cycle, return the one-element array ["Cyclic Dependency"]. Otherwise, if an expression uses a variable that has no definition, return ["Unresolvable equations"]. If both problems occur, the cycle wins.
Operator extension discussion
The exercise starts with addition and then adds subtraction. As a follow-up, explain how you would add multiplication and division. Say how you would handle precedence, division by zero and number precision. This is only for discussion. The tests use just addition and subtraction.
Interviewer Follow-up
In another version of this round, the candidate still uses only addition and subtraction. Instead of one error for the whole list, the candidate computes every variable that can be resolved and lists the ones that cannot. One such input defines the same variable twice, and one definition uses the variable itself. Be ready to explain how you would handle a repeated definition and a self-reference, and how you would report each unresolved variable.
This follow-up is only for discussion. The tests check just the behavior described above.
Function
resolveEquations(equations: String[]) → String[]Examples
Example 1
equations = ["foo = bar + 5", "bar = 2", "abc = 3"]return = ["foo=7", "bar=2", "abc=3"]bar is 2, so foo is 2 + 5 = 7, and abc is 3. The results keep the input order, so the answer is ["foo=7", "bar=2", "abc=3"].
Example 2
equations = ["g = abc + foo", "foo = bar + 5", "bar = 2", "abc = 3"]return = ["g=10", "foo=7", "bar=2", "abc=3"]bar is 2, so foo is 2 + 5 = 7. Then g is 3 + 7 = 10, since abc is 3. The results keep the input order, so the answer is ["g=10", "foo=7", "bar=2", "abc=3"].
Example 3
equations = ["foo = bar + 3", "bar = abc + pqr + 2"]return = ["Unresolvable equations"]Neither abc nor pqr is defined, so the equations cannot be resolved. The answer is ["Unresolvable equations"].
Example 4
equations = ["foo = bar + 3", "bar = foo + 2"]return = ["Cyclic Dependency"]foo depends on bar, and bar depends on foo. That is a cycle, so the answer is ["Cyclic Dependency"].
Example 5
equations = ["foo = bar - 5","bar = 2","abc = foo - -3"]return = ["foo=-3","bar=2","abc=0"]bar = 2 gives foo = 2 - 5 = -3. Then abc = -3 - (-3) = 0. The results keep the input order, so the answer is ["foo=-3","bar=2","abc=0"].
Constraints
1 <= equations.length <= 1000.- Every variable name contains between
1and30lowercase English letters. - Each variable is defined exactly once.
- Each expression contains between
1and100signed-integer or variable terms joined by+or-; every expression follows the documented grammar. - Integer terms are in the range
[-10^9, 10^9]. - For this exercise, assume every intermediate arithmetic result and successfully resolved value fits in a signed 64-bit integer.
More Applied Intuition interview questions
- KV Store with Nested TransactionsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Parse Nested Message TypesPHONE SCREEN · Seen Nov 2026
- Locate Vehicles with Square QueriesPHONE SCREEN · Seen Nov 2026
- File-Backed Dense Game of LifePHONE SCREEN · Seen Oct 2026
- Merge Collinear Point SegmentsPHONE SCREEN · Seen Oct 2026
- Compilation Order with Topological SortPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Merge Overlapping Closed IntervalsPHONE SCREEN · Seen Sep 2026
- Ring Buffer OperationsPHONE SCREEN · Seen Aug 2026
- Out-of-Order TLV File ReassemblyPHONE SCREEN · Seen Aug 2026
- Hash Map with Separate ChainingONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Resolve Variable Equations with Dependency ErrorsMotive · PHONE SCREENMotive interview questions
- Spreadsheet With Dependent CellsCharacter.AI · PHONE SCREEN
- Spreadsheet Formula DependenciesAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Nested Substitutions with Cycle DetectionKikoff · PHONE SCREENKikoff interview questions
- Evaluate Live Spreadsheet ExpressionsStrala · PHONE SCREEN
- Nested Template Variable ExpansionFigma · ONSITE INTERVIEWFigma interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Task Build Order and File-Change RerunsVanta · PHONE SCREEN