Align Temperature Readings with Prior Humidity Data
Arrays & HashingAuto-graded · 17 test casesLast updated
Tests ability to perform temporal joins on sorted time-series data with per-key state tracking and careful timestamp comparison.
You have two chronologically ordered lists of sensor measurements:
- Temperature measurements: Each entry is
[time, city, reading] - Humidity measurements: Each entry is
[time, city, reading]
Your task is to pair each temperature measurement with the corresponding humidity value from the same city. Specifically, for each temperature measurement at time t in city c, find the humidity measurement from city c with the largest timestamp that does not exceed t.
If no humidity measurement from that city has occurred at or before time t, use None (represented as null in JSON output).
Return a list of results aligned with the temperature measurements. Each result should be [time, city, temp_reading, humidity_reading], where humidity_reading is either the matched value or null.
Constraints
- Both input lists are sorted by time in ascending order
0 ≤ len(temperature) ≤ 10^50 ≤ len(humidity) ≤ 10^50 ≤ time ≤ 10^9- City names are non-empty strings
- Readings are numeric values (may be zero or negative)
Examples
Example 1
Input: align_temperature_with_humidity([[1, 'NYC', 12], [5, 'SFO', 20], [7, 'NYC', 23], [16, 'SFO', 10]], [[0, 'SFO', 12], [3, 'SFO', 2], [6, 'NYC', 2], [19, 'SFO', 9]])
Output: [[1, 'NYC', 12, None], [5, 'SFO', 20, 2], [7, 'NYC', 23, 2], [16, 'SFO', 10, 2]]
Hints
Hint 1
Consider how you can use the fact that both lists are already sorted by time
Hint 2
You only need to scan through the humidity list once, tracking the most recent reading for each city
Hint 3
Be careful with the boundary condition: a humidity reading at exactly the same timestamp as a temperature reading should be included
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem requires a temporal join where we match each left-side event (temperature) with the most recent right-side event (humidity) from the same location that occurred at or before the same moment in time.
Since both input lists are already sorted by timestamp, we can solve this efficiently using a two-pointer technique combined with a hash table:
- Maintain a dictionary that stores the most recent humidity reading observed for each city
- Use a pointer to track our position in the humidity list
- For each temperature measurement at time
t:- Advance through the humidity list and consume all measurements with timestamp ≤
t - Update the dictionary with these humidity readings (each city maps to its latest value)
- Look up the current city in the dictionary to find the matched humidity value
- Append the result with the matched value (or
Noneif the city hasn't appeared yet)
- Advance through the humidity list and consume all measurements with timestamp ≤
The key insight is that each humidity measurement is processed exactly once. Because temperature timestamps are non-decreasing and humidity is sorted, once we've consumed a humidity reading for one temperature, it remains available for all subsequent temperatures.
Correctness
The algorithm maintains this invariant: when processing a temperature at time t, our dictionary contains exactly the most recent humidity reading for each city among all humidity records with timestamp ≤ t.
This works because:
- We never move the humidity pointer backward
- Temperature times are non-decreasing, so previously consumed humidity readings remain valid for later temperatures
- Within each city, later humidity readings overwrite earlier ones in the dictionary, ensuring we keep only the most recent
The condition humidity[j][0] <= time (not <) correctly handles cases where humidity and temperature have identical timestamps.
Complexity
Time complexity: O(T + H) where T is the number of temperature measurements and H is the number of humidity measurements. We make a single pass through each list.
Space complexity: O(C) for the dictionary, where C is the number of distinct cities, plus O(T) for the output list.
A naive approach that searches the entire humidity list for each temperature would cost O(T × H), which becomes prohibitively slow for large datasets.
from typing import List
def align_temperature_with_humidity(temperature: List[List], humidity: List[List]) -> List[List]:
latest = {}
j = 0
result = []
for time, city, reading in temperature:
while j < len(humidity) and humidity[j][0] <= time:
h_time, h_city, h_reading = humidity[j]
latest[h_city] = h_reading
j += 1
result.append([time, city, reading, latest.get(city)])
return result