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

Most concentrated month

For each month pick its highest-delay date, breaking ties by earliest date; return the three months with the largest fraction of monthly minutes on that date, rounded to four decimals after ranking.

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 service_date,
          substr(service_date, 1, 7) AS month,
          sum(delay) AS minutes
   FROM incident
   GROUP BY service_date),
     r AS
  (SELECT *,
          sum(minutes) OVER (PARTITION BY month) AS total,
          row_number() OVER (PARTITION BY month
                             ORDER BY minutes DESC, service_date) AS rn
   FROM d)
SELECT month,
       service_date,
       minutes,
       round(1.0*minutes/total, 4) AS fraction
FROM r
WHERE rn=1
ORDER BY 1.0*minutes/total DESC,
         month
LIMIT 3;

Expected output

monthservice_dateminutesfraction
2023-072023-07-249420.1626
2023-022023-02-035260.0999
2023-082023-08-294490.0924

Practise this with a mentor