Code RoomDetect circular references
MediumPrep Room Coding #714

Detect circular references

CodingAlgorithms & data structuresEntry–Mid~16 min

A feature-flag file is lines of the exact form "KEY=value" (split on the first "="). Values may mention other keys with the syntax "${OTHER}". Your linter must reject files where following such mentions can loop forever. Treat each mention of an EXISTING key as an edge from the defining key to the mentioned key; mentions of undefined names are harmless and ignored, as is any malformed "${" without a closing "}". Return true when the resulting graph contains a cycle (a key mentioning itself counts), false otherwise.

Implement
has_circular_reference(lines: list[str]) → bool
Examples
in[["A=${B}","B=${A}"]]outtrue
in[["A=${B}","B=ok"]]outfalse
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 16 min
InputExpectedGot
[["A=${B}","B=${A}"]]truenot run yetsample
[["A=${B}","B=ok"]]falsenot run yetsample