Code Room
CodingMediumcod-g1064
Subject Ml metricsLevel Mid–Senior~25 minCommon in ML systems · Algorithms & data structures interviewsIndustries Software development

Question

Given y_true and y_pred (equal-length lists of integer class labels in 0..k-1) and the number of classes k, compute the per-class precision and recall from the multiclass confusion matrix. For class c, precision = TP_c / (TP_c + FP_c) and recall = TP_c / (TP_c + FN_c), where TP_c is the count of samples with true=c and pred=c, FP_c is true!=c but pred=c, and FN_c is true=c but pred!=c. A zero denominator yields 0.0 for that metric. Return a list of k pairs [precision, recall] for classes 0..k-1, each value rounded to 4 decimals.

Implement
per_class_metrics(y_true: list[int], y_pred: list[int], k: int) → list[list[float]]
Examples
in[[0,1,2,0,1],[0,1,1,0,1],3]out[[1,1],[0.6667,1],[0,0]]
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.