Confusion matrix
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.
0:00 of about 20 min
solution.py
InputExpectedGot
[[0,1,1,0],[0,1,0,0],2][[2,1],[0,1]]not run yetsample