Interview questions · Amazon
Answer a large batch of variable-ratio queries from division equations
Given division equations between named variables and their values, answer a large batch of ratio queries, returning -1.0 whenever a ratio cannot be determined. It tests chaining ratios through shared variables, handling unknown and disconnected variables, and reusing work across many queries, the scale follow-up raised in the interview.
You are given a list of equations between variables. Equation i is equations[i] = [a, b] together with a real number values[i], and it states that a / b = values[i]. Every variable is a string that stands for an unknown positive real number.
For each query [c, d] in queries, return the value of c / d if it can be determined from the equations, or -1.0 if it cannot.
In the interview, the base problem came first. The follow-up asked what changes when the function is called millions of times a day. This version models that follow-up: one call receives one set of equations and a large batch of queries.
Function Signature
def evaluate_ratios(equations: list[list[str]], values: list[float], queries: list[list[str]]) -> list[float]:
Rules
- An equation
a / b = vmay be used in either direction: it also states thatb / a = 1 / v. - A ratio
c / dcan be determined whencanddare linked through a chain of one or more equations; its value is the product of the ratios along the chain. When several chains link the same two variables, they all give the same value, because the equations never contradict each other. - If
corddoes not appear in any equation, the answer is-1.0, even whenc == d. - If
c == dand that variable appears in at least one equation, the answer is1.0. - If both variables appear in equations but no chain links them, the answer is
-1.0. - Return one answer per query, in the order of
queries. - Every determinable ratio is positive, so
-1.0never collides with a real answer. A determinable answer is accepted when its relative error from the exact ratio is at most1e-5. The value-1.0must be returned exactly.
Constraints
1 <= len(equations) == len(values) <= 1000001 <= len(queries) <= 100000len(equations[i]) == 2andlen(queries[j]) == 2- Every variable name has 1 to 5 characters, each a lowercase English letter or a digit.
- In each equation,
a != b. 0.001 <= values[i] <= 1000.0- The equations are consistent: no two chains between the same pair of variables imply different ratios.
- For any two variables linked by a chain, the ratio between them lies between
1e-9and1e9, so no product along a chain overflows or underflows. - Query variables may be names that appear in no equation.
Examples
Example 1
Input: equations = [["x", "y"], ["y", "z"]]
values = [4.0, 2.5]
queries = [["x", "z"], ["z", "x"], ["y", "x"], ["x", "w"], ["y", "y"], ["w", "w"]]
Output: [10.0, 0.1, 0.25, -1.0, 1.0, -1.0]
x / z = (x / y) * (y / z) = 4.0 * 2.5 = 10.0, and z / x is its reciprocal. y / x uses the first equation in reverse. w appears in no equation, so both queries that use it return -1.0, including w / w.
Example 2
Input: equations = [["p", "q"], ["r", "s"], ["t", "q"]]
values = [0.5, 8.0, 0.25]
queries = [["p", "t"], ["t", "p"], ["p", "s"], ["s", "r"]]
Output: [2.0, 0.5, -1.0, 0.125]
p / t = (p / q) * (q / t) = 0.5 * 4.0 = 2.0, using the third equation in reverse. p and s both appear in equations, but no chain links the group {p, q, t} to the group {r, s}, so p / s is -1.0. s / r is the reciprocal of 8.0.
Community answers
Nothing here yet.
Join in on CSWork →