Build a Precision-Recall Curve
Updated
Problem statement
Given equal-length arrays scores and labels, compute precision and recall after each distinct score threshold.
Each label is 0 or 1. Process thresholds in descending score order. All observations with the same score must enter the predicted-positive set together. Return a two-row matrix: row 0 contains precision values and row 1 contains recall values. If there are no positive labels, every recall value is 0.
Function
precisionRecallCurve(scores: double[], labels: int[]) → double[][]Examples
Example 1
scores = [0.9,0.8,0.7,0.1]labels = [1,0,1,0]return = [[1.0,0.5,0.6666666666666666,0.5],[0.5,0.5,1.0,1.0]]Adding examples from highest to lowest score produces cumulative (TP,FP) counts of (1,0), (1,1), (2,1), and (2,2).
Example 2
scores = [0.9,0.9,0.2]labels = [1,0,1]return = [[0.5,0.6666666666666666],[0.5,1.0]]The two observations scoring 0.9 enter together, so the first point is computed after both tied scores are included.
Constraints
1 <= scores.length = labels.length <= 1000000.0 <= scores[i] <= 1.0, and every score is finite.labels[i]is0or1.- Answers are accepted within
10^-9absolute error.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEW
- Optimal Central WarehouseGoogle · ONSITE INTERVIEW
- Pick the K Nearest Grid Points to (0, 0)Capital One · PHONE SCREEN · ONSITE INTERVIEW
- Net Position Calculation from API SnapshotsHeadlands Technologies · ONSITE INTERVIEW
- Activities Within Travel RadiusExpedia · ONSITE INTERVIEW
- Capping Portfolio WeightsCapstone Investment Advisors · OA
- Adaptive Calibration ErrorOpenAI · PHONE SCREEN
- Find All Pythagorean TripletsDell Technologies · PHONE SCREEN
- Maximize Revenue from Decreasing InventoryChubb · OA
- Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREEN