Evaluate Division

Imagine you're building a currency exchange calculator or working with unit conversions. You're given a set of equations that represent division relationships between variables, and you need to evaluate new division queries based on these known relationships.

You have:

  • equations: Array of variable pairs like ["a", "b"]
  • values: Array of numbers where values[i] means equations[i][0] / equations[i][1] = values[i]
  • queries: Array of division questions you need to answer

Goal: For each query [x, y], find the value of x / y. If it can't be determined, return -1.0.

Example: If you know a/b = 2.0 and b/c = 3.0, then you can calculate a/c = 6.0 by following the chain: a/c = (a/b) × (b/c) = 2.0 × 3.0 = 6.0

Input & Output

example_1.py — Basic Division Chain
$ Input: equations = [["a","b"],["b","c"]], values = [2.0,3.0], queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
Output: [6.00000,0.50000,-1.00000,1.00000,-1.00000]
💡 Note: Given: a/b = 2.0, b/c = 3.0. Query a/c: follow path a→b→c, multiply 2.0×3.0 = 6.0. Query b/a: inverse of a/b = 1/2.0 = 0.5. Query a/e: variable 'e' doesn't exist, return -1.0. Query a/a: same variable = 1.0. Query x/x: variable 'x' doesn't exist, return -1.0.
example_2.py — Single Equation
$ Input: equations = [["a","b"]], values = [0.5], queries = [["a","b"],["b","a"],["a","c"],["x","y"]]
Output: [0.50000,2.00000,-1.00000,-1.00000]
💡 Note: Given: a/b = 0.5. Query a/b: direct relationship = 0.5. Query b/a: inverse = 1/0.5 = 2.0. Query a/c: variable 'c' not in any equation, return -1.0. Query x/y: variables 'x' and 'y' don't exist, return -1.0.
example_3.py — Complex Network
$ Input: equations = [["a","b"],["b","c"],["bc","cd"]], values = [1.5,2.5,5.0], queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]
Output: [3.75000,0.40000,5.00000,0.20000]
💡 Note: Given: a/b = 1.5, b/c = 2.5, bc/cd = 5.0. Query a/c: path a→b→c gives 1.5×2.5 = 3.75. Query c/b: inverse of b/c = 1/2.5 = 0.4. Query bc/cd: direct = 5.0. Query cd/bc: inverse = 1/5.0 = 0.2.

Constraints

  • 1 ≤ equations.length ≤ 20
  • equations[i].length == 2
  • 1 ≤ Ai.length, Bi.length ≤ 5
  • values.length == equations.length
  • 0.0 < values[i] ≤ 20.0
  • 1 ≤ queries.length ≤ 20
  • queries[i].length == 2
  • 1 ≤ Cj.length, Dj.length ≤ 5
  • Ai, Bi, Cj, Dj consist of lower case English letters and digits

Visualization

Tap to expand
USD$EURGBP£JPY¥0.851.20130Query: USD → GBPPath: USD → EUR → GBPRate: 0.85 × 1.20 = 1.02Currency Exchange NetworkDFS finds conversion paths and multiplies exchange rates
Understanding the Visualization
1
Build Exchange Network
Create a graph where currencies are nodes and exchange rates are weighted edges
2
Add Bidirectional Rates
For each rate USD/EUR = 0.85, add USD→EUR (0.85) and EUR→USD (1.18)
3
Find Conversion Path
Use DFS to find path from source to destination currency
4
Calculate Final Rate
Multiply exchange rates along the path to get final conversion rate
Key Takeaway
🎯 Key Insight: Model as a weighted graph problem where DFS finds paths and edge weights represent conversion rates that multiply along the path
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28 Uber 22
89.2K Views
Medium-High Frequency
~18 min Avg. Time
3.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen