Z-score outliers, exactly

A free medium coding practice problem from data. Solve it in JavaScript, TypeScript or Python and check your solution against fresh hidden tests — edge cases and random inputs — as often as you like.

Difficulty
Medium
Type
Function
Topic
Data
Languages
JavaScript, TypeScript or Python

Problem

Flag the outliers of values (integers, |v| ≤ 1000, up to 1 000 of them): a value x is an outlier when it lies MORE than k population standard deviations from the mean (k an integer 1–3). To avoid floating-point error, use the equivalent integer test: with n values, S = their sum and Q = the sum of their squares, x is an outlier exactly when

(n·x − S)² > k² · (n·Q − S²)

Return the indices of the outliers in ascending order ([] if none, or if values is empty).

Function signature

// JavaScript
function zscoreOutliers(values, k)

// TypeScript
function zscoreOutliers(values: any, k: any): any

// Python
def zscore_outliers(values, k):

Examples

  1. zscoreOutliers([10,12,11,13,50,12,11], 2) → [4]
  2. zscoreOutliers([1,2,3,4], 1) → [0,3]
  3. zscoreOutliers([5,5,5], 1) → []

How checking works

Run your code against the examples above, then submit a check: it runs on the server against hidden tests built fresh each time, including edge cases and random inputs. Practice is untimed and never affects a verified score; when you are ready, take a graded coding challenge to earn a badge.

More practice problems

Next steps