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 · Cohorts

Severe-delay recurrence within 30 days

For station labels whose first 15+ minute date was on or before December 1, count labels with another severe date within the following 30 days; return cohort size and recurrence percentage rounded to two decimals.

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),
     r AS
  (SELECT station,
          service_date,
          lead(service_date) OVER (PARTITION BY station
                                   ORDER BY service_date) AS next_date,
          row_number() OVER (PARTITION BY station
                             ORDER BY service_date) AS rn
   FROM d)
SELECT count(*) AS cohort,
       sum(CASE
               WHEN next_date<=date(service_date, '+30 days') THEN 1
               ELSE 0
           END) AS recurred,
       round(100.0*sum(CASE
                           WHEN next_date<=date(service_date, '+30 days') THEN 1
                           ELSE 0
                       END)/count(*), 2) AS recurrence_pct
FROM r
WHERE rn=1
  AND service_date<='2023-12-01';

Expected output

cohortrecurredrecurrence_pct
1025049.02

Practise this with a mentor