Evaluate Division
Updated
Problem statement
You are given variable pairs equations and positive real numbers values. For every index i, the pair equations[i] = [a, b] and value values[i] mean that a / b = values[i].
You are also given pairs queries. For each query [c, d], determine the value of c / d.
Return one answer for each query in the same order. Return -1.0 when the answer cannot be determined.
The equations are valid and mutually consistent. A variable that never appears in equations is undefined, even when it is queried against itself.
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]Multiplying a / b = 2.0 by b / c = 3.0 gives a / c = 6.0. Flipping the first equation gives b / a = 0.5. Variables e and x never appear in the equations, so they are undefined and the queries that use them have no answer.
Example 2
equations = [["a","b"],["b","c"],["bc","cd"]]values = [1.5,2.5,5.0]queries = [["a","c"],["c","b"],["bc","cd"],["cd","bc"]]return = [3.75,0.4,5.0,0.2]The first two equations give a / c = 1.5 * 2.5 = 3.75 and c / b = 1 / 2.5 = 0.4. Here bc and cd are variable names of their own. Their equation is listed directly, so bc / cd is its listed value and cd / bc is that value flipped.
Example 3
equations = [["a","b"]]values = [0.5]queries = [["a","b"],["b","a"],["a","c"],["x","y"]]return = [0.5,2.0,-1.0,-1.0]The equation gives a / b = 0.5 directly, and flipping it gives b / a = 2.0. The last two queries use c, x and y, which never appear in the equations, so they have no answer.
Constraints
1 ≤ equations.length ≤ 20equations[i].length = 2values.length = equations.length0.0 < values[i] ≤ 20.01 ≤ queries.length ≤ 20queries[i].length = 2- Every variable name has length from
1through5and contains only lowercase English letters and digits. - The input contains no contradictory equations and no division by zero.
More InMobi Phone Screen questions
- House Robber IPHONE SCREEN · Seen Aug 2026
- Word Break IIPHONE SCREEN · Seen Aug 2026
- Bus Station Seat AllotmentPHONE SCREEN · Seen Mar 2026
- Find Missing and Duplicate IntegerPHONE SCREEN · Seen Mar 2026
- Frequency Equals Card ValuePHONE SCREEN · Seen Mar 2026
- Frog Position After T SecondsPHONE SCREEN · Seen Mar 2026
- IPOPHONE SCREEN · Seen Mar 2026
- Compare Version NumbersPHONE SCREEN · Seen Feb 2022
- Count Array DerangementsPHONE SCREEN · Seen Feb 2022
- Maximum Frequency StackPHONE SCREEN · Seen Feb 2022
Similar questions at other companies
- Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Evaluate DivisionApple · PHONE SCREENApple interview questions
- Possible BipartitionByteDance · PHONE SCREENByteDance interview questions
- Route Graph Reachability with a Broken LinkFigma · PHONE SCREENFigma interview questions
- Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Number of IslandsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Count Connected IslandsUber · PHONE SCREENUber interview questions
- Course ScheduleNykaa · PHONE SCREENNykaa interview questions
- Number of IslandsSpaceX · PHONE SCREENSpaceX interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions