FastPrepMean Binary Focal Loss
Problems/Mean Binary Focal Loss

Mean Binary Focal Loss

Datadog logoDatadog● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand MLE report names Focal Loss and GQA in a 45-minute handwritten coding screen, without an interface or framework. This exercise supplies the binary alpha-balanced focal-loss formula, 0/1 labels, probability inputs, mean reduction, empty-batch rule and bounds. These are practice details; the original framework is unknown. The named task matches the report at about 70%.

Compute the mean binary focal loss for a batch. probabilities[i] is the predicted probability of class 1, and labels[i] is the true class.

For each example, choose pt = p and weight = alpha when its label is 1. When the label is 0, choose pt = 1 - p and weight = 1 - alpha. Its loss is:

loss = -weight × (1 - pt)^gamma × ln(pt)

Return the sum of these losses divided by the number of examples, including examples whose class weight is zero. Here ln is the natural logarithm, and a positive number raised to power zero is one. For an empty batch, return 0. The supplied values are probabilities, rather than logits; do not apply a sigmoid.

Function

binaryFocalLoss(probabilities: double[], labels: int[], alpha: double, gamma: double) → double

Examples

Example 1

probabilities = [0.5,0.5]labels = [1,0]alpha = 0.25gamma = 2return = 0.08664339756999316

Both true-class probabilities are 0.5. Their class weights are 0.25 and 0.75, and the focusing factor is 0.25. The two losses sum to 0.25 × ln(2); dividing by two gives 0.125 × ln(2), about 0.08664339757.

Example 2

probabilities = [0.8,0.2]labels = [1,0]alpha = 0.5gamma = 0return = 0.11157177565710488

Both true-class probabilities are 0.8. With gamma = 0, the focusing factor is one. Each example has loss -0.5 × ln(0.8), so their mean is about 0.11157177566.

Example 3

probabilities = []labels = []alpha = 0.25gamma = 2return = 0.0

The practice empty-batch rule returns zero without dividing by the batch size.

Constraints

  • 0 <= probabilities.length <= 200
  • probabilities.length == labels.length
  • 0.000001 <= probabilities[i] <= 0.999999
  • Every label is the integer 0 or 1.
  • 0 <= alpha <= 1
  • 0 <= gamma <= 5
  • All numeric inputs are finite.

More Datadog problems

  • Query Log MatchPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Delete a Filesystem TreePHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
  • Grouped-Query Attention Forward PassPHONE SCREEN · Seen Jun 2026
All Datadog interview questionsSee Datadog hiring insights
Saved
public double binaryFocalLoss(double[] probabilities, int[] labels, double alpha, double gamma) {
    // Write your code here.
}
Checking account…