cswork / libraryOpen practice workspace

Coding questions · Two Sigma

Analyze NYC taxi trips efficiently over last 7 days

This question evaluates proficiency in time-series filtering, relational joins, grouped aggregations (median and high-percentile calculations), numeric stability of percentile methods, and performance-aware vectorized data manipulation using SQL or pandas.

Use today = 2025-09-01. Consider NYC taxi trip data over the last 7 days inclusive (2025-08-26 to 2025-09-01, America/New_York). You receive two datasets and must write a fast, vectorized analysis (no Python for-loops over rows). Data schema and tiny samples:

trips(id, taxi_id, pickup_ts, dropoff_ts, pickup_zone_id, dropoff_zone_id, distance_miles, fare_amount)
 1 | 101 | 2025-08-26 08:15 | 2025-08-26 08:45 | 1 | 3 | 6.0 | 18.50
 2 | 102 | 2025-08-26 00:20 | 2025-08-26 00:50 | 2 | 1 | 4.0 | 14.00
 3 | 101 | 2025-08-28 01:10 | 2025-08-28 01:40 | 1 | 1 | 3.0 | 12.00
 4 | 103 | 2025-08-30 17:05 | 2025-08-30 17:25 | 4 | 2 | 2.5 | 9.50
 5 | 104 | 2025-09-01 02:30 | 2025-09-01 03:20 | 1 | 4 | 10.0 | 30.00
 6 | 102 | 2025-08-31 23:50 | 2025-09-01 00:10 | 3 | 3 | 5.0 | 16.00
zones(zone_id, borough)
 1 | Manhattan
 2 | Brooklyn
 3 | Queens
 4 | Bronx

Tasks:

  1. After joining trips with zones on pickup_zone_id, compute per (borough, hour_of_day from pickup_ts) the median trip speed in mph, where speed = distance_miles / duration_hours. Filter trips to 1 ≤ duration_minutes ≤ 120 and 1 ≤ speed ≤ 80. Return the top 3 (borough, hour) pairs by median speed; break ties by borough asc, then hour asc. Report the exact (borough, hour, median_speed_mph) triples. 2) For trips with pickup borough = 'Manhattan' and pickup time between 00:00 and 05:00 inclusive, identify the 3 taxi_id with the largest 95th percentile of trip duration (minutes) over the same date range; break ties by taxi_id asc. Clearly define how you compute the 95th percentile (e.g., pandas/numpy method) and use a stable, vectorized approach. 3) Provide pandas code (or SQL) that runs in O(n log n) or better due to grouping/quantile operations, avoids per-row loops, and uses: parsed datetime dtypes; one-to-many join performed once; categorical dtype for borough; appropriate indexing on pickup_ts for time filtering. 4) Briefly justify two memory/performance optimizations you employ (e.g., downcasting floats/ints, using groupby-agg with quantile in a single pass, avoiding intermediate copies).

Top 3 (borough, hour) by median NYC taxi trip speed (last 7 days)

You are given NYC taxi trips and a zone-to-borough mapping. Consider trips with pickup_ts in the inclusive date range 2025-08-26 to 2025-09-01 (filter as pickup_ts >= '2025-08-26 00:00:00' and pickup_ts < '2025-09-02 00:00:00').

Join trips to zones on trips.pickup_zone_id = zones.zone_id. For each (borough, hour_of_day) where hour_of_day is extracted from pickup_ts (0-23), compute the median trip speed in mph, where:

  • duration_minutes = (dropoff_ts - pickup_ts) in minutes
  • duration_hours = duration_minutes / 60
  • speed_mph = distance_miles / duration_hours

Filter to trips with 1 <= duration_minutes <= 120 and 1 <= speed_mph <= 80.

Return the top 3 (borough, hour_of_day) pairs by median speed (descending). Break ties by borough ascending, then hour_of_day ascending.

Output columns: borough, hour_of_day, median_speed_mph.

Table zones

columntypeconstraints
zone_idINTPRIMARY KEY, NOT NULL
boroughVARCHAR(50)NOT NULL

Sample data:

boroughzone_id
Manhattan1
Brooklyn2
Queens3
Bronx4

Table trips

columntypeconstraints
idINTPRIMARY KEY, NOT NULL
taxi_idINTNOT NULL
pickup_tsTIMESTAMPNOT NULL
dropoff_tsTIMESTAMPNOT NULL
pickup_zone_idINTNOT NULL
dropoff_zone_idINTNOT NULL
distance_milesDECIMAL(6,2)NOT NULL
fare_amountDECIMAL(8,2)NOT NULL

Sample data:

distance_milesdropoff_tsdropoff_zone_idfare_amountidpickup_tspickup_zone_idtaxi_id
62025-08-26 08:45:00318.512025-08-26 08:15:001101
42025-08-26 00:50:0011422025-08-26 00:20:002102
32025-08-28 01:40:0011232025-08-28 01:10:001101
2.52025-08-30 17:25:0029.542025-08-30 17:05:004103
102025-09-01 03:20:0043052025-09-01 02:30:001104
52025-09-01 00:10:0031662025-08-31 23:50:003102
82025-08-27 05:40:0022572025-08-27 04:50:001105
92025-08-29 00:55:0032682025-08-29 00:05:001101
4.52025-08-31 05:30:00213.592025-08-31 05:00:001104
122025-08-30 04:30:00435102025-08-30 03:00:001106

Expected output

Top 3 borough/hour pairs ranked by median speed (mph) after filtering by duration and speed constraints.

boroughhour_of_daymedian_speed_mph
Queens2315
Manhattan212
Manhattan812

Hints

  1. Compute duration using EXTRACT(EPOCH FROM (dropoff_ts - pickup_ts)) and derive speed from it.
  2. Use PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY speed_mph) to get the median per group.
  3. Filter the pickup_ts range as [2025-08-26, 2025-09-02) to include all of 2025-09-01.

Top 3 Manhattan late-night taxis by 95th percentile trip duration

Using the same tables, consider trips with pickup_ts in the inclusive date range 2025-08-26 to 2025-09-01 (filter as pickup_ts >= '2025-08-26 00:00:00' and pickup_ts < '2025-09-02 00:00:00').

Filter to trips where the pickup borough is 'Manhattan' (join on pickup_zone_id) AND the pickup time-of-day is between 00:00:00 and 05:00:00 inclusive.

For each taxi_id, compute the 95th percentile of trip duration in minutes using a continuous percentile definition (i.e., SQL PERCENTILE_CONT(0.95)).

Return the 3 taxi_id with the largest 95th percentile duration. Break ties by taxi_id ascending.

Output columns: taxi_id, p95_duration_minutes.

Table zones

columntypeconstraints
zone_idINTPRIMARY KEY, NOT NULL
boroughVARCHAR(50)NOT NULL

Sample data:

boroughzone_id
Manhattan1
Brooklyn2
Queens3
Bronx4

Table trips

columntypeconstraints
idINTPRIMARY KEY, NOT NULL
taxi_idINTNOT NULL
pickup_tsTIMESTAMPNOT NULL
dropoff_tsTIMESTAMPNOT NULL
pickup_zone_idINTNOT NULL
dropoff_zone_idINTNOT NULL
distance_milesDECIMAL(6,2)NOT NULL
fare_amountDECIMAL(8,2)NOT NULL

Sample data:

distance_milesdropoff_tsdropoff_zone_idfare_amountidpickup_tspickup_zone_idtaxi_id
62025-08-26 08:45:00318.512025-08-26 08:15:001101
42025-08-26 00:50:0011422025-08-26 00:20:002102
32025-08-28 01:40:0011232025-08-28 01:10:001101
2.52025-08-30 17:25:0029.542025-08-30 17:05:004103
102025-09-01 03:20:0043052025-09-01 02:30:001104
52025-09-01 00:10:0031662025-08-31 23:50:003102
82025-08-27 05:40:0022572025-08-27 04:50:001105
92025-08-29 00:55:0032682025-08-29 00:05:001101
4.52025-08-31 05:30:00213.592025-08-31 05:00:001104
122025-08-30 04:30:00435102025-08-30 03:00:001106

Expected output

Top 3 taxi_id values ranked by the continuous 95th percentile of trip duration (minutes) for Manhattan pickups between 00:00 and 05:00 inclusive.

p95_duration_minutestaxi_id
90106
50105
49101

Hints

  1. Use pickup_ts::time (or an equivalent time extraction function) to filter the 00:00:00–05:00:00 window.
  2. Use PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY duration_minutes) for a continuous percentile.
  3. Apply the date filter before grouping to keep the computation efficient.