KatInterview

Batch Ratio Lookup Over Linked Quantities

Trees & GraphsAuto-graded · 15 test cases
MediumAuto-gradedTrees & Graphsunion-findgraphweighted-union-find

Last updated

Answer many ratio queries between named quantities from pairwise ratio facts, using a weighted union-find or component precomputation instead of a search per query.

Batch Ratio Lookup Over Linked Quantities

Several named quantities are related by pairwise ratio facts. Fact number i involves the pair pairs[i] = [a, b] and the number factors[i], and it says that the quantity a is factors[i] times the quantity b, that is, a / b = factors[i]. Each name stands for some unknown positive real number.

You are then given a list asks of questions [c, d]. For each one, report the value of c / d if the facts determine it, and -1.0 otherwise.

Implement the function so that it handles a large batch of questions in a single call.

Rules

  • A fact can be used in both directions. If a / b = v, then b / a = 1 / v.
  • c / d is determined when c and d are joined by a chain of one or more facts, each sharing a name with the next. Its value is the product of the ratios along the chain, with each fact used in the direction that fits. All chains between the same two names give the same value, because the facts never contradict each other.
  • If c or d appears in no fact at all, the answer is -1.0. This holds even when c == d.
  • If c == d and that name appears in at least one fact, the answer is 1.0.
  • If both names appear in facts but no chain joins them, the answer is -1.0.
  • Return exactly one answer per question, in the same order as asks.
  • Every determined ratio is positive, so -1.0 can never be confused with a real answer. A determined answer is accepted if its relative error is at most 1e-5. The value -1.0 must be returned exactly.

Constraints

  • 1 <= len(pairs) == len(factors) <= 100000
  • 1 <= len(asks) <= 100000
  • len(pairs[i]) == 2 and len(asks[j]) == 2
  • Every name has 1 to 5 characters, each a lowercase letter or a digit.
  • In each fact, the two names differ.
  • 0.001 <= factors[i] <= 1000.0
  • The facts are consistent: two chains between the same pair of names never imply different ratios.
  • For any two names joined by a chain, the ratio between them lies between 1e-9 and 1e9, so no product along a chain overflows or underflows.
  • Names in asks may be names that appear in no fact.

Examples

Example 1

Input: batch_ratio_lookup([['x', 'y'], ['y', 'z']], [4, 2.5], [['x', 'z'], ['z', 'x'], ['y', 'x'], ['x', 'w'], ['y', 'y'], ['w', 'w']])

Output: [10, 0.1, 0.25, -1, 1, -1]

Example 2

Input: batch_ratio_lookup([['p', 'q'], ['r', 's'], ['t', 'q']], [0.5, 8, 0.25], [['p', 't'], ['t', 'p'], ['p', 's'], ['s', 'r']])

Output: [2, 0.5, -1, 0.125]

Hints

Hint 1

Treat each name as a node and each fact as an edge that carries a ratio. Running a fresh search for every question is far too slow when there are 100,000 of them, so aim to do the heavy work once.

Hint 2

Within one connected group, you can express every name as a multiple of a single representative name. Then any ratio inside the group is just one stored number divided by another. A union-find structure that stores each node's ratio to its parent can maintain this.

Hint 3

When merging two groups through a fact a / b = v, work out the ratio between the two roots from each name's ratio to its root. Keep path compression iterative, since a chain of 100,000 facts will overflow Python's recursion limit. Check that both names are known before calling find.