Sensor anomaly polling: count out-of-range readings and track each sensor's min and max

Quick Overview

Given readings from several sensors and a valid range, return for each sensor how many readings fall strictly outside the range, plus its minimum and maximum reading. Tests single-pass array processing with constant extra memory, inclusive boundary handling and the empty-sensor edge case.

Sensor anomaly polling: count out-of-range readings and track each sensor's min and max

Company: Spacex

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are writing firmware-level code that polls several sensors. Each sensor produces a stream of readings over time. You are given a 2D array `readings`, where `readings[i]` holds every reading from sensor `i`, and a valid range `[low, high]`. A reading is an anomaly when it falls strictly outside the range: it is strictly less than `low` or strictly greater than `high`. A reading equal to `low` or `high` is valid. For each sensor, compute the number of anomalies, the minimum reading and the maximum reading. To simulate a constrained memory environment, process each sensor's data in a single pass without storing intermediate filtered results. Return a list with one entry per sensor, in the same order as `readings`, where each entry is `[anomaly_count, min_reading, max_reading]`. If a sensor has no readings, its entry is `[0, 0, 0]`. ### Function Signature ```python def poll_sensors(readings: list[list[int]], low: int, high: int) -> list[list[int]]: ``` ### Rules - The output has exactly `len(readings)` entries, and entry `i` describes sensor `i`. - The minimum and maximum are taken over all readings of the sensor, anomalies included. - A sensor with an empty list of readings maps to `[0, 0, 0]`. ### Constraints - `1 <= len(readings) <= 1000` - `0 <= len(readings[i]) <= 10^4` - `-10^6 <= readings[i][j] <= 10^6` - `-10^6 <= low <= high <= 10^6` ### Examples ```text Input: readings = [[10, 25, 50, 75, 100], [5, 15, 30, 45, 60], [200, -50, 0]], low = 10, high = 60 Output: [[2, 10, 100], [1, 5, 60], [3, -50, 200]] ``` Sensor 0 has two readings above 60 (75 and 100), and 10 equals `low`, so it is valid. Sensor 1 has one reading below 10 (the 5), while 60 equals `high`. Sensor 2 has three anomalies: 200 is above 60, and both -50 and 0 are below 10. ```text Input: readings = [[20, 30, 40], []], low = 10, high = 50 Output: [[0, 20, 40], [0, 0, 0]] ``` Every reading of sensor 0 lies inside [10, 50]. Sensor 1 has no readings, so its entry is [0, 0, 0]. ```text Input: readings = [[-3], [7, 7, 8]], low = 7, high = 7 Output: [[1, -3, -3], [1, 7, 8]] ``` Sensor 0 has a single reading, -3, which is below 7, so it is one anomaly and also both the minimum and the maximum. In sensor 1 the two 7s sit exactly on both bounds and are valid; only the 8 is an anomaly.

Overview: Given readings from several sensors and a valid range, return for each sensor how many readings fall strictly outside the range, plus its minimum and maximum reading. Tests single-pass array processing with constant extra memory, inclusive boundary handling and the empty-sensor edge case.

Spacex logo
Spacex
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are writing firmware-level code that polls several sensors. Each sensor produces a stream of readings over time. You are given a 2D array readings, where readings[i] holds every reading from sensor i, and a valid range [low, high].

A reading is an anomaly when it falls strictly outside the range: it is strictly less than low or strictly greater than high. A reading equal to low or high is valid.

For each sensor, compute the number of anomalies, the minimum reading and the maximum reading. To simulate a constrained memory environment, process each sensor's data in a single pass without storing intermediate filtered results.

Return a list with one entry per sensor, in the same order as readings, where each entry is [anomaly_count, min_reading, max_reading]. If a sensor has no readings, its entry is [0, 0, 0].

Function Signature

def poll_sensors(readings: list[list[int]], low: int, high: int) -> list[list[int]]:

Rules

  • The output has exactly len(readings) entries, and entry i describes sensor i .
  • The minimum and maximum are taken over all readings of the sensor, anomalies included.
  • A sensor with an empty list of readings maps to [0, 0, 0] .

Constraints

  • 1 <= len(readings) <= 1000
  • 0 <= len(readings[i]) <= 10^4
  • -10^6 <= readings[i][j] <= 10^6
  • -10^6 <= low <= high <= 10^6

Examples

Input: readings = [[10, 25, 50, 75, 100], [5, 15, 30, 45, 60], [200, -50, 0]], low = 10, high = 60
Output: [[2, 10, 100], [1, 5, 60], [3, -50, 200]]

Sensor 0 has two readings above 60 (75 and 100), and 10 equals low, so it is valid. Sensor 1 has one reading below 10 (the 5), while 60 equals high. Sensor 2 has three anomalies: 200 is above 60, and both -50 and 0 are below 10.

Input: readings = [[20, 30, 40], []], low = 10, high = 50
Output: [[0, 20, 40], [0, 0, 0]]

Every reading of sensor 0 lies inside [10, 50]. Sensor 1 has no readings, so its entry is [0, 0, 0].

Input: readings = [[-3], [7, 7, 8]], low = 7, high = 7
Output: [[1, -3, -3], [1, 7, 8]]

Sensor 0 has a single reading, -3, which is below 7, so it is one anomaly and also both the minimum and the maximum. In sensor 1 the two 7s sit exactly on both bounds and are valid; only the 8 is an anomaly.