Classify Temperatures into Three Weather Bands
Arrays & HashingAuto-graded · 18 test casesLast updated
Tests careful boundary handling by bucketing integer temperatures into cold, mild, and hot ranges and returning the three counts in order.
A weather station records daily high temperatures (in °F) for a set of locations. Your job is to sort those readings into three weather bands and report how many locations fall into each one.
The bands are defined as follows:
- Cold: temperature strictly below 50
- Mild: temperature from 50 up to and including 80
- Hot: temperature strictly above 80
Given a list of integer temperature readings, return a list of three integers representing the count of cold readings, mild readings, and hot readings, in that order.
Rules
- A reading of exactly 50 or exactly 80 belongs to the mild band.
- A reading of exactly 81 belongs to the hot band.
- Every reading falls into exactly one band, so the three counts always sum to the total number of readings.
- An empty list of readings must return
[0, 0, 0].
Constraints
0 <= len(temps) <= 1000000 <= temps[i] <= 100
Examples
Example 1
Input: classify_temperatures([30, 50, 65, 80, 81, 95, 49])
Output: [2, 3, 2]
Example 2
Input: classify_temperatures([100, 100])
Output: [0, 0, 2]
Example 3
Input: classify_temperatures([])
Output: [0, 0, 0]
Hints
Hint 1
A single pass through the list is all you need — track one counter per band as you go.
Hint 2
Write your comparisons as an if / elif / else chain so each reading increments exactly one counter. Three separate if statements risk counting a boundary value twice.
Hint 3
The trickiest inputs are the exact boundary values: 49, 50, 80, and 81. Make sure your comparison operators (< vs <=) place each of those in the right band, then check the empty list as a final edge case.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each temperature maps to exactly one of three buckets, determined by two threshold values (50 and 80). One pass through the list with three running counters is all that is needed.
The key insight is to express the classification as an if / elif / else chain rather than three independent if statements:
- If
t < 50→ cold. - Otherwise (meaning
t >= 50), ift <= 80→ mild. The lower bound is already guaranteed by the first check failing. - Everything remaining (meaning
t > 80) → hot.
This structure ensures every reading increments exactly one counter.
Why the boundaries are correct
| Value | First check (t < 50) | Second check (t <= 80) | Band |
|---|---|---|---|
| 49 | true | — | cold |
| 50 | false | true | mild |
| 80 | false | true | mild |
| 81 | false | false | hot |
The spec says 50 and 80 are mild and 81 is hot — the table confirms this.
Edge cases
- Empty list: the loop body never executes, all counters stay at zero, and
[0, 0, 0]is returned without any special-case code. - All readings in one band: the other two counters simply remain at zero.
- Extreme values:
0is cold (below 50) and100is hot (above 80), both handled by the open-ended first and last branches.
Common mistakes to avoid
- Using
s <= 50for the cold check puts 50 in the cold bucket instead of mild. - Using
s < 80for the mild check puts 80 in the hot bucket instead of mild. - Using three separate
ifstatements with overlapping or loose conditions can count a value twice or miss it entirely. - Returning the counts in the wrong order, or as a tuple instead of a list.
Complexity
- Time: O(n) — each reading is examined once with at most two comparisons.
- Space: O(1) extra — only three integer counters are maintained; the output is always a fixed-length list of three.
O(n) time is optimal here because every reading must be examined at least once to determine its band.
Reference solution
from typing import List
def classify_temperatures(temps: List[int]) -> List[int]:
cold = mild = hot = 0
for t in temps:
if t < 50:
cold += 1
elif t <= 80:
mild += 1
else:
hot += 1
return [cold, mild, hot]
Alternative worth knowing
You can compute one count by subtraction once the other two are known:
cold = sum(1 for t in temps if t < 50)
hot = sum(1 for t in temps if t > 80)
mild = len(temps) - cold - hot
This is still O(n) but makes two passes and leans on the partition property to avoid a third loop. The single-pass version above is cleaner and more direct.