Code Room
CodingMediumcod-g986
Subject Ml metricsLevel Mid–Senior~20 minCommon in ML systems · Algorithms & data structures interviewsIndustries Software development, Technology

Question

Build a confusion matrix for a multiclass problem. Given equal-length lists of predicted and true integer labels and an integer num_classes (labels range over 0..num_classes-1), return a num_classes x num_classes matrix M where M[t][p] is the number of samples whose true label is t and predicted label is p. Rows are indexed by true label, columns by predicted label. Inputs may be empty (return an all-zero matrix of the right shape).

Implement
confusion_matrix(preds: list[int], labels: list[int], num_classes: int) → list[list[int]]
Examples
in[[0,1,1,0],[0,1,0,0],2]out[[2,1],[0,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.