Evaluate Division
Updated
Problem statement
You are given equations between variables: equations[i] = [a, b] and values[i] together mean a / b = values[i]. Every value is positive. For example, ["ft", "in"] with value 12 describes a unit conversion: a foot is 12 inches.
For each query [c, d] in queries, return the value of c / d that follows from the equations. Return -1.0 when it cannot be worked out:
corddoes not appear in any equation (this includes a query[x, x]whenxis unknown), or- no chain of equations links
ctod.
A query [x, x] returns 1.0 when x appears in at least one equation. Return the answers in the same order as queries. An answer is accepted when it is within 10-5 × max(1, |expected|) of the expected value.
Function
calcEquation(equations: List<List<String>>, values: double[], queries: List<List<String>>) → double[]Examples
Example 1
equations = [["a","b"],["b","c"]]values = [2.0,3.0]queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]return = [6.0,0.5,-1.0,1.0,-1.0]a / c = (a / b) × (b / c) = 2 × 3 = 6, andb / a = 1 / 2 = 0.5.eandxnever appear in an equation, soa / eandx / xare-1.0.a / a = 1becauseais known.
Example 2
equations = [["yd","ft"],["ft","in"],["lb","oz"]]values = [3.0,12.0,16.0]queries = [["yd","in"],["oz","lb"],["yd","lb"]]return = [36.0,0.0625,-1.0]yd / in = 3 × 12 = 36: a yard is 3 feet and a foot is 12 inches.oz / lb = 1 / 16 = 0.0625.ydandlbare in different groups that no equation connects, so the answer is-1.0.
Example 3
equations = [["x1","x2"]]values = [0.5]queries = [["x2","x1"]]return = [2.0]x2 / x1 = 1 / 0.5 = 2.
Constraints
1 <= equations.length <= 20equations[i].length == 2andqueries[j].length == 2values.length == equations.length0.0 < values[i] <= 20.01 <= queries.length <= 20- Every variable name has 1 to 5 characters, each a lowercase English letter or a digit.
- The two variables in one equation are different, and the equations never contradict each other.
More Plaud Onsite Interview questions
Similar questions at other companies
- Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Determine Whether Two Horses Are Genetically RelatedGoogle · ONSITE INTERVIEWGoogle interview questions
- Redundant ConnectionMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Count Connected Components in an Undirected GraphNutanix · ONSITE INTERVIEWNutanix interview questions
- Connected GroupsFivetran · ONSITE INTERVIEWFivetran interview questions
- Largest Island After One Land ConversionAmazon · ONSITE INTERVIEW
- Making a Large IslandMeta · ONSITE INTERVIEWMeta interview questions
- Largest Island After One Land ConversionBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Evaluate DivisionApple · PHONE SCREENApple interview questions
- Minimum Unreachable WarehousesVisa · OAVisa interview questions