Reported June 2024
Motivedepth first search

Resolve Variable Equations with Dependency Errors

Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Motive OA. Under 2s to a working solution.
Founder's read

Motive reportedly asked this one in June 2024, and it looks like a string parsing problem until you spot the graph underneath. You get up to 1000 variable definitions, each with up to 100 terms, and every term is an integer or another variable. Naive re-expansion of each variable blows up fast when definitions chain. The real task is dependency resolution with cycle detection, plus a strict error precedence rule. If you blank on the DFS state handling during the OA, StealthCoder runs invisibly on your screen as a safety net. Know the shape before you sit down.

The problem

You are given an array of variable definitions. Each definition has the form name = term + term +.... A term is either a signed integer or the name of another variable. Variable names contain only lowercase English letters. Whitespace may appear around names, integers, =, and +.
Resolve every variable and return one string per definition, in the same order as the input. Each successful result must have the form name=value.
If the dependency graph among defined variables contains any cycle, return the one-element array ["Cyclic Dependency"]. Otherwise, if any expression references a variable that is not defined, return ["Unresolvable equations"]. A cycle takes precedence when both conditions occur.

Function
resolveEquations(equations: String[]) → String[]

Examples
Example 1
equations = ["foo = bar + 5", "bar = 2", "abc = 3"]
return = ["foo=7", "bar=2", "abc=3"]
bar resolves to 2, so foo resolves to 2 + 5 = 7. Results retain definition order.
Example 2
equations = ["g = abc + foo", "foo = bar + 5", "bar = 2", "abc = 3"]
return = ["g=10", "foo=7", "bar=2", "abc=3"]
Depth-first dependency resolution gives foo = 7, then g = 3 + 7 = 10.
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.
Example 4
equations = ["foo = bar + 3", "bar = foo + 2"]
return = ["Cyclic Dependency"]
foo depends on bar, and bar depends on foo, forming a cycle.

Constraints
1 <= equations.length <= 1000
Every variable name contains between 1 and 30 lowercase English letters.
Each variable is defined exactly once.
Each expression contains between 1 and 100 terms joined by +.
Integer terms are in the range [-10^9, 10^9].
Every successfully resolved value fits in a signed 64-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Parse each line into a name and a list of terms, trimming whitespace and splitting on plus. Watch the signed integers, since a term like -5 or +3 is still a number. Build a map from name to terms. Run DFS with three states: unvisited, visiting, done. Memoize each resolved value so every variable is computed once, which kills the brute force blowup from re-expanding chains. Hitting a visiting node means a cycle. The pitfall is precedence: a cycle beats an undefined reference. So don't return early on a missing variable. Detect cycles across the whole graph first, or record the missing flag and keep going, then decide at the end. Use 64-bit longs. If the live OA has you freezing on the precedence logic, StealthCoder is the hedge that gets you unstuck without anyone seeing it.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Resolve Variable Equations with Dependency Errors cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Motive's OA.

Motive reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Resolve Variable Equations with Dependency Errors FAQ

What's the trick in the Motive resolve equations problem?+

Treat it as a dependency graph. Parse each definition, then DFS with memoization and three-state coloring (unvisited, visiting, done). A back edge to a visiting node is a cycle. Memoizing means each variable resolves once, so long chains don't get recomputed.

How do I handle cycle versus unresolvable precedence?+

Cycle wins. Don't return Unresolvable the moment you see an undefined name. Keep a missing flag, finish the DFS over every defined variable, and return Cyclic Dependency if any cycle exists. Only if there's no cycle and the flag is set do you return Unresolvable equations.

How should I parse the whitespace and signed integers?+

Split on the first equals sign, trim the name, then split the right side on plus and trim each term. A term is an integer if it parses as one, including a leading minus. Otherwise it's a variable name. Test with extra spaces around every token.

Do I need to worry about overflow?+

The constraints say resolved values fit in signed 64-bit, but individual terms go up to 10^9 and you add up to 100 of them. Use long in Java or C++. Python is fine. Don't use 32-bit ints for the accumulator or intermediate sums.

How do I prepare for this in 48 hours?+

Practice one graph DFS with cycle detection and one memoized expression evaluator. Then write this problem end to end once, including the parser. Focus on the three-state visited array and the error precedence, since those are where most attempts break.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Motive.

OA at Motive?
Invisible during screen share
Get it