Evaluate division
You maintain n variables and process a list of operations. Each operation is either ["set", a, b, w] meaning the ratio value(a)/value(b) equals the float w, or ["q", a, b] meaning 'return the value of value(a)/value(b)'. For each query, return the ratio if a and b are connected by previously set relations, otherwise return -1.0. Queries where a == b in a known component return 1.0. Return the list of query answers (floats), each rounded to 5 decimal places.
Implement
weight_queries(n: int, ops: list) → list[float]Examples
in
[4,[["set",0,1,2],["set",1,2,3],["q",0,2],["q",0,3]]]out[6,-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
[4,[["set",0,1,2],["set",1,2,3],["q",0,2],["q",0,3]]][6,-1]not run yetsample