Winsorize Outlier Values

~15 mincode completion

Implement winsorize(arr, lower_pct, upper_pct) using and .

Examples

Single high outlier clipped to 75th percentile

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

Both tails clipped: low values raised, high values lowered

Input
winsorize([0, 10, 20, 30, 40, 50], 20, 80)
Output
[10, 10, 20, 30, 40, 40]

All identical values: clipping leaves array unchanged

Input
winsorize([5, 5, 5, 5], 25, 75)
Output
[5, 5, 5, 5]

Hints

Hint 1

bounds an array in one call.

Hint 2

Reach for percentile rather than min max.

Requirements

  • arr: 1D float array

  • lower_pct: Lower percentile (0-100)

  • upper_pct: Upper percentile (0-100)

  • Return Winsorized array of the same shape.

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 winsorize(arr: np.ndarray, lower_pct: float, upper_pct: float) -> np.ndarray:
    """
    Clip values to the [lower_pct, upper_pct] percentile range.

    Args:
        arr:       1D float array
        lower_pct: Lower percentile (0-100)
        upper_pct: Upper percentile (0-100)

    Returns:
        Winsorized array of the same shape.
    """
    # 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.