Your question is Top-K Anomaly Detection. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
A Gulfstream aircraft health monitoring stream contains an unsorted array of integer sensor readings. Define each reading's anomaly score as its absolute difference from the median of all readings. Return the original indices of the k readings with the largest anomaly scores.
If multiple readings have the same score, prefer the smaller original index. Return indices ordered by decreasing anomaly score, then increasing index.
Implement top_k_anomalies(readings, k). The input readings is a non-empty list of integers, and k is an integer from 1 through len(readings). Return a list of exactly k integer indices.
For an even number of readings, calculate the median as the arithmetic mean of the two middle values after sorting. A reading equal to the median has an anomaly score of zero.
def top_k_anomalies(readings, k):