IQR Outlier Detection

~15 mincode completion

Implement iqr_outlier_mask(arr) that returns an integer array where 1 = outlier and 0 = normal.

Examples

Single high spike flagged at position 4

Input
iqr_outlier_mask([1, 2, 3, 4, 100])
Output
[0, 0, 0, 0, 1]

All identical values: IQR is zero, no outliers

Input
iqr_outlier_mask([10, 10, 10, 10, 10])
Output
[0, 0, 0, 0, 0]

Single low outlier flagged at position 0

Input
iqr_outlier_mask([-100, 2, 3, 4, 5])
Output
[1, 0, 0, 0, 0]

Hints

Hint 1

Work directly with the arguments arr and return the result rather than printing it.

Hint 2

Reach for 1 5 rather than wrong fence multiplier e g 3.

Requirements

  • arr: 1D float array

  • Return Integer array of same shape: 1 where arr is outside the IQR fences, else 0.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~15 min

8 employers weight this skill

2 health and bio companies, 2 AI product companies, 1 defense company, 1 enterprise vendor, 1 quant fund, 1 big tech firm. Top match scores 87.

Python
import numpy as np

def iqr_outlier_mask(arr: np.ndarray) -> np.ndarray:
    """
    Return an integer mask (1 = outlier, 0 = normal) using the 1.5*IQR rule.

    Args:
        arr: 1D float array

    Returns:
        Integer array of same shape: 1 where arr is outside the IQR fences, else 0.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.