KatInterview

Align Temperature Readings with Prior Humidity Data

Arrays & HashingAuto-graded · 17 test cases
EasyAuto-gradedArrays & Hashingtime-seriestwo-pointershash-table

Last 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^5
  • 0 ≤ len(humidity) ≤ 10^5
  • 0 ≤ 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