Winsorize the upper tail of a heavy-tailed metric with NumPy

Quick Overview

Implement a NumPy function that caps the upper tail of a heavy-tailed per-user metric at a chosen quantile while keeping every value in its original order. It tests quantile interpolation, vectorized array handling and how outlier capping fits into A/B test analysis.

Winsorize the upper tail of a heavy-tailed metric with NumPy

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: easy

Interview Round: Other

Implement `def winsorize_upper(x: np.ndarray, upper_pct: float) -> np.ndarray`. `x` is a heavy-tailed per-user metric. Cap (do not drop) the upper tail and return the capped array. Requirements: - Compute `cap = np.quantile(x, upper_pct)` with the default (linear) interpolation. - Return `np.minimum(x, cap)`: only values strictly above the cap change, and the lower tail is unchanged. - The output is `float64` with the same length and element order as `x` (nothing dropped or reordered). Example: ```python x = np.array([1.0, 2.0, 3.0, 4.0, 100.0]) winsorize_upper(x, 0.90) # cap = np.quantile(x, 0.90) = 61.6 # Output: [1.0, 2.0, 3.0, 4.0, 61.6] ``` ```hint Check the contract Trace the example by hand to see where linear interpolation puts the cap, then ask what your code returns for an integer array or a plain list. Length, order and dtype are all part of the contract. ``` ### Constraints & Assumptions - `upper_pct` is a fraction in `(0, 1)`, for example `0.99`. - `x` is a non-empty 1-D `float64` array. ### What a Strong Answer Covers - Computing the cap with the specified quantile and interpolation method on the full array. - Applying the cap elementwise so the length, the order and the lower tail are preserved, without mutating the caller's array. - Returning `float64` output and stating the time and space cost. - Edge cases such as ties at the top, NaN values and very small arrays. - What capping preserves compared with dropping outliers, in terms of sample size, the randomization unit and variance. ### Follow-up Questions - Why winsorize instead of dropping outliers? - How would you apply this in an A/B test without introducing bias? - If the treatment mainly changes the behavior of the heaviest users, what does capping do to the measured effect?

Overview: Implement a NumPy function that caps the upper tail of a heavy-tailed per-user metric at a chosen quantile while keeping every value in its original order. It tests quantile interpolation, vectorized array handling and how outlier capping fits into A/B test analysis.

General logo
General
Nov 25, 2026
easyData ScientistOtherAnalytics & Experimentation
0
0

Implement def winsorize_upper(x: np.ndarray, upper_pct: float) -> np.ndarray. x is a heavy-tailed per-user metric. Cap (do not drop) the upper tail and return the capped array.

Requirements:

  • Compute cap = np.quantile(x, upper_pct) with the default (linear) interpolation.
  • Return np.minimum(x, cap) : only values strictly above the cap change, and the lower tail is unchanged.
  • The output is float64 with the same length and element order as x (nothing dropped or reordered).

Example:

x = np.array([1.0, 2.0, 3.0, 4.0, 100.0])
winsorize_upper(x, 0.90)
# cap = np.quantile(x, 0.90) = 61.6
# Output: [1.0, 2.0, 3.0, 4.0, 61.6]

Constraints & Assumptions

  • upper_pct is a fraction in (0, 1) , for example 0.99 .
  • x is a non-empty 1-D float64 array.

What a Strong Answer Covers Guidance

  • Computing the cap with the specified quantile and interpolation method on the full array.
  • Applying the cap elementwise so the length, the order and the lower tail are preserved, without mutating the caller's array.
  • Returning float64 output and stating the time and space cost.
  • Edge cases such as ties at the top, NaN values and very small arrays.
  • What capping preserves compared with dropping outliers, in terms of sample size, the randomization unit and variance.

Follow-up Questions Guidance

  • Why winsorize instead of dropping outliers?
  • How would you apply this in an A/B test without introducing bias?
  • If the treatment mainly changes the behavior of the heaviest users, what does capping do to the measured effect?