You are given a list of equations and a matching list of values. Equation i is a pair of variable names [a, b] and says that a divided by b equals values[i]. Each variable is a short name made of lower-case letters and digits.
You are also given queries, each a pair [c, d]. For each query, work out c / d from the equations. If the equations do not pin it down, including when c or d never appears in any equation, the answer is −1.0. Return the answers in query order.
The equations never contradict each other, and no value is zero.
Example 1
Input: equations = [["x","y"],["y","z"]], values = [2.0, 4.0],
queries = [["x","z"],["z","x"],["y","y"],["y","w"],["q","q"]]
Output: [8.0, 0.125, 1.0, -1.0, -1.0]
Why: x/z = 2 × 4; w and q appear in no equation2 more examples and the constraints
Example 2
Input: equations = [["p","q"],["r","s"]], values = [4.0, 2.0],
queries = [["p","s"],["s","r"],["q","p"]]
Output: [-1.0, 0.5, 0.25]
Why: p and s are in groups that never meetExample 3
Input: equations = [["ab","cd"]], values = [2.5], queries = [["cd","ab"]] Output: [0.4] Why: flipping a division flips the value: 1 / 2.5
Constraints
- 1 ≤ number of equations ≤ 20, and 1 ≤ number of queries ≤ 20
- 0 < values[i] ≤ 20
- Variable names are 1 to 5 lower-case letters or digits
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Graph + DFS per queryO(Q·(V + E))
- Weighted Union-FindO((E + Q)·α)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 20 equations and 20 queries | Verdict |
|---|---|---|---|
| Graph + DFS per query | O(Q·(V + E)) | Passes; easy to explain | |
| Weighted Union-Find | O((E + Q)·α) | Best; each query near O(1) |
V = variables, E = equations, Q = queries; α is the near-constant Union-Find factor.
Edge cases to test
- Unknown variable over itself. q / q is −1 when q is in no equation, even though any number over itself is 1.
- Known variable over itself. y / y is 1 as soon as y appears in some equation.
- Separate groups. Two variables that are both known but never linked give −1.
- Reverse direction. b / a needs the reverse edge with weight 1 / v; forgetting it breaks every flipped query.
Common mistakes
- Returning 1.0 for x / x when x was never in an equation.
- Adding only the a → b edge and not b → a with the inverse weight.
- In Union-Find, updating the parent during find before multiplying in the old parent’s ratio.
How to talk through it
- 1Clarify. Can a query use a variable that is in no equation? Can the equations contradict each other?
- 2Name the graph. Variables are nodes; a / b = v is an edge a → b of weight v and b → a of weight 1 / v.
- 3Answer with a DFS. Multiply weights along the path; no path means −1. With 20 equations this is plenty.
- 4Offer Union-Find. Store each variable as a multiple of its root, so a query is one division after two finds.
- 5Test out loud. Run an unknown x / x, a flipped query and two separate groups before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?