Precision and recall at k from a flaky top-k API
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Microsoft's technical phone screen, last asked Feb 2026. It tests evaluation metrics, retries and apis.
You have 10 image files, each with a ground-truth label saying whether it contains a dog. An API search_dogs(k) is supposed to return the k file ids the system thinks are most likely dogs. Write code that computes the precision and recall of what the API returns against the labels. Then make the computation robust to an API that misbehaves.
Part 1: the metric
Unless you agree on a different interface with the interviewer, assume:
-
labels: dict[str, bool]— file id to "is a dog". -
evaluate(search_dogs, labels, k) -> dict— returnsprecision,recalland the list of results it used. - Precision is true positives over the number of results used; recall is true positives over the number of dogs in
labels.
labels = {"img0": False, "img1": True, "img2": True, "img3": False, "img4": True,
"img5": False, "img6": False, "img7": True, "img8": False, "img9": False}
search_dogs(4) returns ["img1", "img3", "img4", "img5"]
evaluate(search_dogs, labels, 4) -> precision 0.5, recall 0.5 # 2 of 4 hits, 2 of 4 dogs
Part 2: a flaky API
The API may return None, raise, time out, return fewer or more than k ids, repeat ids, or return ids that are not in labels. Make the result well defined and say what you report.
One reasonable policy: retry failures a few times with back-off; drop unknown ids and duplicates (keeping first occurrences) and count them; keep at most the first k; return None for precision when nothing valid came back; report every anomaly alongside the numbers.
search_dogs(4) returns ["img1", "img1", "img99", "img2"]
evaluate(...) -> used ["img1", "img2"], precision 1.0, recall 0.5,
anomalies {"duplicates": 1, "unknown_ids": 1, "short_by": 2}
Clarifying questions
- Is precision measured over what was returned, or over
k(missing results count as misses)? - What should recall be when there are no dogs at all?
- Should a failed call count as zero results, or be excluded and reported?
- Does the order of results matter (precision at each cut-off, average precision)?
What a strong answer covers
- Correct definitions using sets of ids, and the division-by-zero cases made explicit.
- A validation layer between the API and the metric, with each anomaly counted rather than silently fixed.
- Retries with a limit and back-off, and a clear outcome when every attempt fails.
- Stating the choice between "precision over returned" and "precision at k", since they differ when the API returns fewer results.
- Small tests for each misbehaviour.
Follow-ups
- Compute the metrics for many queries and aggregate them (micro versus macro averages).
- The API is non-deterministic and returns different results on each call. How do you report that?
- Add average precision or NDCG using the order of results.
Where it's asked
- Microsoft · Technical phone screen · Last asked Feb 202660 min · One technical round
What it tests
evaluation metrics · retries · apis
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
