Brief
Choose a join strategy for the table sizes and prevent false links from a key reused over time.
Voss waves a table of seven 'transport proofs' for four trips. Imani notices D7 changed name at 20:00. Mara asks you to rebuild the join on a copy before the Hidden Depot map is shown to the team. The suspense belongs to the story; the exercise has no timer.
Your mission
- Explain why joining only on depot_id produces seven rows.
- Match T1, T2, T3, and T4 to the place valid at their times.
- Choose between broadcasting a tiny depot table and sort-merging two large tables, naming a cost of each.
- Give a check that immediately detects false matches.
How to check your work
- Three D7 trips × two versions + one D8 trip = seven naive rows.
- T1 North; T2 and T3 Hidden Depot; T4 South.
- Broadcast saves moving the large side but uses memory; sort-merge moves and sorts data.
- Four distinct trip_id values → four rows, one dated description per trip.
Course example
trips = [
("T1", "D7", 1170),
("T2", "D7", 1285),
("T3", "D7", 1300),
("T4", "D8", 1310),
]
depots = [
("D7", "North Yard", 0, 1200),
("D7", "Hidden Depot", 1200, 1440),
("D8", "South Yard", 0, 1440),
]
naive = [(trip[0], depot[1]) for trip in trips for depot in depots if trip[1] == depot[0]]
temporal = [
(trip[0], depot[1])
for trip in trips for depot in depots
if trip[1] == depot[0] and depot[2] <= trip[2] < depot[3]
]
print("naive_rows", len(naive))
print("temporal_rows", len(temporal))
print("temporal_matches", temporal)
print("one_match_per_trip", len({trip_id for trip_id, _ in temporal}) == len(trips))