KatInterview

Best Recall Cutoff That Meets a Minimum Precision Target

Heaps, Sorting & SearchAuto-graded · 18 test cases
MediumAuto-gradedHeaps, Sorting & Searchsortingprecision-recallbinary-classification

Last updated

Tests threshold search on a scored ranking: find the cut-point that maximises recall while keeping precision at or above a given floor, with correct tie-breaking.

Best Recall Cutoff That Meets a Minimum Precision Target

A binary classifier outputs a numeric confidence score for each item. You flag an item as positive whenever its score is at or above some cutoff τ. Raising τ flags fewer items; lowering it flags more.

Given the true labels and scores from a held-out validation set, find the cutoff that maximises recall among all cutoffs where precision is at least min_precision. Break ties in recall by preferring the lower cutoff (flags more items while keeping precision in bounds). If multiple cutoffs tie on both recall and the lower-cutoff rule, any one of them is acceptable.

Return a dictionary with three keys:

  • "threshold" — the chosen cutoff value (a float from the input scores)
  • "precision" — exact precision at that cutoff
  • "recall" — exact recall at that cutoff

If there are no positive labels in the validation set, or if no cutoff achieves precision ≥ min_precision, return {"threshold": None, "precision": None, "recall": None}.

Definitions

For a given cutoff τ:

  • Predicted positives (PP): items whose score ≥ τ
  • True positives (TP): items that are both predicted positive and actually positive
  • precision = TP / PP (undefined when PP = 0; such a cutoff is never a valid candidate)
  • recall = TP / (total actual positives)

Tie-group rule

Items that share the same score value must be treated as a unit. Either all of them are above the cutoff or none of them are. When computing precision and recall at cutoff τ, include every item whose score equals τ.

Constraints

  • 1 ≤ len(labels) ≤ 50 000
  • len(labels) == len(scores)
  • Each element of labels is 0 or 1
  • 0.0 < min_precision ≤ 1.0
  • Scores are finite floats; duplicates are allowed
  • The "flag nothing" operating point (PP = 0) is never a valid candidate because its precision is undefined

Examples

Example 1

Input: find_best_recall_cutoff([1, 0, 1, 0], [0.9, 0.8, 0.7, 0.1], 0.95)

Output: {'threshold': 0.9, 'precision': 1, 'recall': 0.5}

Example 2

Input: find_best_recall_cutoff([1, 0, 1, 0], [0.9, 0.8, 0.7, 0.1], 0.5)

Output: {'threshold': 0.1, 'precision': 0.5, 'recall': 1}

Hints

Hint 1

Sort items by descending score and walk the list once, accumulating true-positive and predicted-positive counts. Each distinct score value is one candidate cutoff — flush the counts only after processing the whole tie group at that score.

Hint 2

Precision is not guaranteed to decrease monotonically as you lower the cutoff. You cannot stop at the first violation; you must scan every distinct score and collect every candidate that meets the precision floor.

Hint 3

Store feasible candidates as (recall, -threshold, ...) so that a single call to max() gives the highest recall first, then the lowest threshold as the tie-breaker.