Skip to content
DeepForgeby svx
All problems
MediumML Fundamentalsml-127

Anomaly Bounds by IQR

Problem

Compute IQR anomaly bounds: [Q1 - k*IQR, Q3 + k*IQR]. Q1 is the median of the lower half and Q3 the median of the upper half (for odd length, exclude the median element).

Empty input returns [0.0, 0.0].

Hint

The usual k is 1.5; values outside the returned interval are flagged as anomalies.

Test cases

case 1
in: [[1,2,3,4,5,6,7,8],1.5]
expected: [-3.5,12.5]
case 2
in: [[1,2,3,4,5],1.5]
expected: [-3,9]
case 3
in: [[7,7,7],1.5]
expected: [7,7]
case 4
in: [[1,2,3,4],1.5]
expected: [-1.5,6.5]
case 5
in: [[],1.5]
expected: [0,0]
Starter codePython
def iqr_bounds(values, k):
    # Returns [lower, upper]
    # Your code here
    pass

5 test cases · run your code against them in the editor