Code RoomMost frequent confusion pair
MediumPrep Room Coding #544

Most frequent confusion pair

CodingML systemsAlgorithms & data structuresEntry–Mid~14 min

The moderation team wants to know the model's single worst confusion: which (true category, predicted category) mistake happens most often. Given equal-length string lists y_true and y_pred, consider only the positions where the two differ. Count the occurrences of each ordered (true, predicted) pair and return the count of the most frequent pair. If the model made no mistakes (or the lists are empty), return 0. Example: y_true = ["spam", "ok", "spam", "toxic", "spam"], y_pred = ["ok", "ok", "ok", "spam", "ok"] returns 3 (the pair spam predicted as ok).

Implement
top_confusion(y_true: list[str], y_pred: list[str]) → int
Examples
in[["spam","ok","spam","toxic","spam"],["ok","ok","ok","spam","ok"]]out3
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 14 min
InputExpectedGot
[["spam","ok","spam","toxic","spam"],["ok","ok","ok","spam","ok"]]3not run yetsample