Evaluate division
You are given equations as pairs of variable names with a known ratio: equations[i] = [A, B] and values[i] means A / B = values[i]. Given a list of queries [C, D], return for each query the value of C / D, or -1.0 if it cannot be determined from the given equations (unknown variable or disconnected). Use only the relations provided. Answers within 1e-5 of the true value are accepted; return the exact computed float.
Implement
evaluate_division(equations: list[list[str]], values: list[float], queries: list[list[str]]) → list[float]Examples
in
[[["a","b"],["b","c"]],[2,3],[["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]]out[6,0.5,-1,1,-1]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 35 min
solution.py
InputExpectedGot
[[["a","b"],["b","c"]],[2,3],[["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]][6,0.5,-1,1,-1]not run yetsample