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

Longest station reporting gaps

For stations with at least 20 distinct reporting dates, show the five longest elapsed gaps between consecutive reporting dates; break ties by station and later 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),
     a AS
  (SELECT *,
          lag(service_date) OVER (PARTITION BY station
                                  ORDER BY service_date) AS previous_date,
          count(*) OVER (PARTITION BY station) AS n
   FROM d)
SELECT station,
       previous_date,
       service_date,
       cast(julianday(service_date)-julianday(previous_date) AS INTEGER) AS days_between
FROM a
WHERE n>=20
  AND previous_date IS NOT NULL
ORDER BY days_between DESC,
         station,
         service_date
LIMIT 5;

Expected output

stationprevious_dateservice_datedays_between
SCARBOROUGH CTR STATIO2023-08-072023-10-2175
DAVISVILLE YARD2023-07-032023-08-2755
KENNEDY SRT TO LAWRENC2023-04-252023-06-1753
WILSON YARD2023-03-162023-05-0853
DAVISVILLE YARD2023-01-272023-03-1446

Practise this with a mentor