Meta Data#
Difficulty: easy First Attempt: 2021-10-10
- Submissions: Runtime Error → Accepted
Approach#
Join each day to the previous day (SQL Template 3) with DATEDIFF(w.recordDate, y.recordDate) = 1 and filter w.temperature > y.temperature. Adjacent ids do not mean adjacent dates: ids need not follow date order, and dates may be missing.
My Attempts#
Attempt 1 mistyped the join as JOIN weather,w, with a stray comma (RE); removing it passed.
Solution#
SELECT w.id
FROM Weather w
JOIN Weather y ON DATEDIFF(w.recordDate, y.recordDate) = 1
WHERE w.temperature > y.temperature;
Complexity#
A self join, near O(n) with a date index.
Mistakes to Avoid#
- Determine the previous day by date, not by id.
DATEDIFFis MySQL-specific; other databases differ (for exampledate - interval).
Interview Explanation#
I self-join Weather where the dates differ by exactly one day and keep ids warmer than the previous day; the previous day must be found by date, since id order need not follow dates.
