Code RoomUnweighted loss ignores class imbalance
MediumPrep Room Coding #4229

Unweighted loss ignores class imbalance

Vibe & agenticAlgorithms & data structuresMid–Senior~17 min

An AI wrote this PyTorch training step for a 1:200 imbalanced defect-detection model. Loss decreases smoothly but the model predicts 'no defect' almost always. Explain why and fix it.

Implement
select_f1_threshold(scores: list[float], labels: list[int], thresholds: list[float]) → float
Examples
in[[0.1,0.2,0.05,0.4],[0,1,0,1],[0.5,0.3,0.15]]out0.15
in[[0.2,0.7],[0,0],[0.9,0.5]]out0.9
in[[0.42,0.38,0.44,0.05,0.02],[1,0,1,0,0],[0.5,0.4]]out0.4
What a strong answer looks like

Treat the AI’s output as a draft to verify, not an answer to trust. Name the specific flaw and the input that triggers it, say how you’d catch it (tests, edge cases, reading critically), and how you’d re-prompt or decompose to get it right.

0:00 of about 17 min

Vibe & agentic: describe the solution in plain language (or narrate it) and the coach grades your approach.

Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.