Sensor Median Uses Upper Middle Value for Even Samples
Level 2 · node · Correctness · Missing average of two middle values for even sample counts
Scenario
A fictional greenhouse dashboard combines temperature readings from multiple sensors in a zone and displays the median. The result looks correct for odd numbers of sensors, but with an even count the service selects the higher of the two middle readings instead of their average. Inputs may be unsorted.
Requirements
Sort numeric readings ascending. For an odd count return the single middle reading; for an even count return the arithmetic mean of the two middle readings. Return null for an empty input and do not mutate the original array.
Acceptance criteria
- Odd-sized input returns its middle value.
- Even-sized input returns the average of the two middle values.
- Unsorted even-sized input is handled correctly.
- Empty input returns null.