Your question is Sliding Window Aggregation in Python. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Lyft receives a chronological stream of completed ride events. For each event, report the number of rides and total fare for that city during the trailing time window ending at the event timestamp.
An event is represented as [timestamp, city, fare]. The window is left-exclusive and right-inclusive: for timestamp t and window size w, include events with timestamp > t - w and timestamp <= t. Each event produces one output record after it is added to the window.
Implement aggregate_ride_metrics(events, window_seconds).
events is a list of [int timestamp, string city, number fare] entries sorted by nondecreasing timestamp.window_seconds is a positive integer.timestamp, city, ride_count, and total_fare.total_fare should preserve the numeric type resulting from adding the input fares.def aggregate_ride_metrics(events, window_seconds):