Code RoomEquation division queries
HardPrep Room Coding #1065

Equation division queries

CodingAlgorithms & data structuresSenior–Staff~35 min

You are given equations as pairs of variable names and a parallel list of values, where equations[i] = [Ai, Bi] and values[i] = k means Ai / Bi = k (all values positive). For each query [Cj, Dj], compute Cj / Dj if it is determinable from the equations, otherwise return -1.0 for that query. A query involving a variable that never appears in any equation returns -1.0. Return the list of answers in query order. There are at most 200 equations and 200 queries.

Implement
evaluate_ratios(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"],["x","x"]]]out[6,0.5,-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
InputExpectedGot
[[["a","b"],["b","c"]],[2,3],[["a","c"],["b","a"],["a","e"],["x","x"]]][6,0.5,-1,-1]not run yetsample