Code Room
CodingEasycod-g1340
Subject Ml metricsLevel Entry–Mid~10 minCommon in ML systems interviewsIndustries Software development

Question

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]]) → int
Examples
in[[3,1,2],[[3,5],[2,4],[2,1]]]out2
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.