Problem
You get a list of equations and a list of values. equations[i] = [A, B] and values[i] mean that A / B = values[i]. Each A and each B is a variable name, which is a string.
You also get a list of queries. queries[j] = [C, D] asks for the value of C / D. Use the equations to find each answer.
Return a list with one answer for each query. If the equations do not give the answer, use -1.0. A variable that is not in any equation has no known value, so a query with it gets -1.0.
The equations do not contradict each other, and no equation divides by zero.
Example 1
- Input:
- equations = [["x","y"],["y","z"]], values = [4,0.5], queries = [["x","z"],["z","x"],["y","y"],["x","w"],["w","w"]]
- Output:
- [2.0, 0.5, 1.0, -1.0, -1.0]
- Explanation:
x / zis(x / y) * (y / z), which is4.0 * 0.5 = 2.0. The variablewis not in any equation.
Example 2
- Input:
- equations = [["p","q"],["r","s"]], values = [4,2], queries = [["q","p"],["p","s"]]
- Output:
- [0.25, -1.0]
- Explanation:
- No chain of equations connects
pands.
Example 3
- Input:
- equations = [["ab","cd"]], values = [2.5], queries = [["cd","ab"],["ab","ab"],["cd","ef"]]
- Output:
- [0.4, 1.0, -1.0]
Constraints
- 1 <= equations.length <= 20
- equations[i].length == 2
- 1 <= A.length, B.length <= 5
- values.length == equations.length
- 0.0 < values[i] <= 20.0
- 1 <= queries.length <= 20
- queries[i].length == 2
- 1 <= C.length, D.length <= 5
- A, B, C and D have only lowercase English letters and digits.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Accounts Merge
Coding · Array · Breadth-First Search · Depth-First Search
- Alien Dictionary
Coding · Array · Breadth-First Search · Depth-First Search
- Minimize Malware Spread
Coding · Array · Breadth-First Search · Depth-First Search
- Smallest String With Swaps
Coding · Array · Breadth-First Search · Depth-First Search
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 2 records
Amazon
Leetcode 399. Evaluate Division
Reported Jun 9, 2025 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View sourceBloomberg
Leetcode 399. Evaluate Division
Reported Sep 1, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 1 record
Bloomberg
Evaluate Division
Reported date not specified · Source window: 3mo
Source snapshot: Oct 20, 2026
Source frequency score: 61.8. This is not an interview probability.
View source