DIY: Evaluate Division

Solve the interview question "Evaluate Division" yourself in this lesson.

We'll cover the following

Problem statement

You are given equations, an array of variable pairs, and values, an array of real numbers. equations[i] = [Ai, Bi] and values[i] represent the equation Ai / Bi = values[i]. Each Ai or Bi is a string that represents a single variable.

You also have some queries, where queries[j] = [Cj, Dj] represents the jth query, and you must find the answer for Cj / Dj = ?.

Return the answers to all queries. If a single answer cannot be determined, return -1.0.

Input

The following is an example input:

equations = [["a","b"],["b","c"]]
values = [2.0,3.0]
queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]

Output

The following is an example output:

[6.0, 0.5, -1.0, 1.0, -1.0]

Coding exercise

For this coding exercise, you need to implement the evaluate(equations, values, queries) function. The function will return the answers for every variable pair in the queries array.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.