Intent classifier hits
A chatbot's intent classifier returns a short ranked list of candidate intent ids for each utterance, and the eval counts a hit when the true intent appears anywhere in that list (top-k accuracy). Given y_true, a list of true intent ids, and topk_preds, where topk_preds[i] is the candidate list for utterance i, return the number of utterances whose true id appears in their candidate list. Example: y_true = [3, 1, 2] with topk_preds = [[3, 5], [2, 4], [2, 1]] has hits at positions 0 and 2, so return 2.
Implement
topk_hits(y_true: list[int], topk_preds: list[list[int]]) → intExamples
in
[[3,1,2],[[3,5],[2,4],[2,1]]]out2What 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 10 min
solution.py
InputExpectedGot
[[3,1,2],[[3,5],[2,4],[2,1]]]2not run yetsample