Data Interview Prep
Problems are free. Session requests and mentor applications are saved and the owner follows up by email. Nothing is charged on this site.

All problems · Windows

Consecutive severe days at a station

Find the five longest streaks of consecutive dates at one station label with at least one delay of 15+ minutes on each date; break ties by station then start date.

Source

Data: TTC Subway Delay Data (City of Toronto, 2023 snapshot), licence, read 2026-11-29.

Schema

incident(id INTEGER, service_date TEXT, clock_time TEXT, weekday TEXT, station TEXT, code TEXT, delay INTEGER, gap INTEGER, bound TEXT, line TEXT, vehicle INTEGER); delay_code(code TEXT, description TEXT). One incident row is one workbook record, not necessarily a unique event. Dates are ISO YYYY-MM-DD; id is the 1-based data row ordinal. Delay and gap are recorded minutes; gap is time between trains. Blank bound and line values remain NULL; zero values and raw labels are preserved. IDs identify source rows, not riders.

Contains information licensed under the Open Government Licence – Toronto.
Show the checked solution

SQL

WITH d AS
  (SELECT DISTINCT station,
                   service_date
   FROM incident
   WHERE delay>=15),
     g AS
  (SELECT *,
          julianday(service_date)-row_number() OVER (PARTITION BY station
                                                     ORDER BY service_date) AS island
   FROM d)
SELECT station,
       min(service_date) AS first_date,
       max(service_date) AS last_date,
       count(*) AS streak
FROM g
GROUP BY station,
         island
ORDER BY streak DESC,
         station,
         first_date
LIMIT 5;

Expected output

stationfirst_datelast_datestreak
EGLINTON STATION2023-12-192023-12-224
ISLINGTON STATION2023-03-062023-03-083
KENNEDY SRT STATION2023-06-152023-06-173
LANSDOWNE STATION2023-01-232023-01-253
BAY STATION2023-10-202023-10-212

Practise this with a mentor