← Microsoft Interview Insights
This one took me longer than I expected to get right.
Start by clarifying the problem and edge cases, then outline a threshold-sweeping algorithm that sorts scores descending and accumulates TP/FP counts. Write clean code that handles ties by grouping equal scores and edge cases like no positives or no predicted positives, and optionally compute Average Precision using the trapezoidal rule.
Pro tip: Mention that sorting scores descending and processing ties as a single threshold avoids duplicate points and ensures a monotonic PR curve, which is crucial for correct AP calculation. Also, explicitly state how you handle division by zero when precision or recall is undefined.
Confirm input formats, whether scores are probabilities, and how to handle edge cases like no positive labels, no predicted positives, and tied scores. Discuss the desired output (list of (precision, recall) points) and whether to compute AP.
Sort scores in descending order along with labels. Sweep thresholds from high to low, maintaining cumulative TP and FP counts. For tied scores, process all at once to avoid intermediate points. Compute precision and recall at each distinct threshold.
Write code that initializes TP=0, FP=0, and iterates through sorted scores. Handle division by zero: if no predicted positives, precision is undefined (often set to 1 or 0 depending on convention); if no actual positives, recall is undefined. Ensure the curve includes the starting point (recall=0, precision=1) and ending point (recall=1, precision=total_positives/total_samples).
If requested, compute AP as the area under the PR curve using the trapezoidal rule or the step-wise method (sum of precision at each threshold times change in recall). Mention that AP is equivalent to AUC-PR and is useful for imbalanced datasets.
Test with small examples: all positives, all negatives, perfect predictions, random predictions, and tied scores. Verify that the curve is monotonic (precision non-increasing as recall increases) and that AP matches expectations.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.