Code Room
CodingHardcod-g279
Subject Union findLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.