Compute and plot a precision–recall curve

Read the full interview experience this question came from →

Quick Overview

This implementation-level question evaluates understanding of precision–recall curve computation and related binary classification metrics, including threshold sweeping, ranking by predicted score, handling ties, and edge cases such as no predicted positives or datasets with zero positives; Category/Domain: Coding & Algorithms, Data Science (Position: Data Scientist). It is commonly asked because it verifies practical ability to build evaluation pipelines and reason about classifier performance under class imbalance and scoring ties, and to extend results to aggregate measures like Average Precision / PR-AUC.

Compute and plot a precision–recall curve

Company: Microsoft

Role: Data Scientist

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

You are given model outputs for a binary classifier: - `y_true`: an array of 0/1 ground-truth labels of length `n`. - `y_score`: an array of predicted scores/probabilities in `[0, 1]` of length `n`. Write code (language of your choice) to generate the **precision–recall (PR) curve**. Requirements: 1. Sort examples by `y_score` descending and sweep a threshold from high to low. 2. At each unique threshold (or each rank), compute: - `precision = TP / (TP + FP)` - `recall = TP / (TP + FN)` 3. Return arrays of `(recall_points, precision_points)` suitable for plotting. 4. Handle edge cases: - no predicted positives (define precision as 1 or 0—state your convention), - ties in scores, - datasets with zero positives. Optional follow-up: compute **Average Precision (AP)** / PR-AUC from the curve.

Overview: This implementation-level question evaluates understanding of precision–recall curve computation and related binary classification metrics, including threshold sweeping, ranking by predicted score, handling ties, and edge cases such as no predicted positives or datasets with zero positives; Category/Domain: Coding & Algorithms, Data Science (Position: Data Scientist). It is commonly asked because it verifies practical ability to build evaluation pipelines and reason about classifier performance under class imbalance and scoring ties, and to extend results to aggregate measures like Average Precision / PR-AUC.

Read the full Microsoft Data Scientist interview experience this question came from

Community answers

Answer by muzizhuyou

y_true_scores = [(y, score) for y, score in zip(y_true.tolist(), y_score.tolist())] y_true_scores.sort(key=lambda x: x[1], reverse = True) y_scores = [x[1] for x in y_true_scores] y_true = [x[0] for x in y_true_scores] recall_precision = [(0,1)] # edge case n= len(y_scores) total_positive = sum([x for x in y_true if x==1]) for i in range(n): #new_scores = [1](i+1) + [0](n-i-1) TP = len([x for x in y_true[:i+1] if x == 1]) if total_positive == 0: precision = 0 recall = 0 recall_precision.append((recall, precision)) continue precision = TP /(i+1) recall = TP / total_positive recall_precision.append((recall, precision)) import matplotlib.pyplot as plt recalls = [x[0] for x in recall_precision] precisions = [x[1] for x in recall_precision] plt.plot(recalls, precisions) plt.xlabel("Recall") plt.ylabel("Precision") plt.show()
|Home/Coding & Algorithms/Microsoft
Microsoft logo
Microsoft
Nov 24, 2025
easyData ScientistTechnical ScreenCoding & Algorithms
10
0

You are given model outputs for a binary classifier:

  • y_true : an array of 0/1 ground-truth labels of length n .
  • y_score : an array of predicted scores/probabilities in [0, 1] of length n .

Write code (language of your choice) to generate the precision–recall (PR) curve.

Requirements:

  1. Sort examples by y_score descending and sweep a threshold from high to low.
  2. At each unique threshold (or each rank), compute:
    • precision = TP / (TP + FP)
    • recall = TP / (TP + FN)
  3. Return arrays of (recall_points, precision_points) suitable for plotting.
  4. Handle edge cases:
    • no predicted positives (define precision as 1 or 0—state your convention),
    • ties in scores,
    • datasets with zero positives.

Optional follow-up: compute Average Precision (AP) / PR-AUC from the curve.

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...