Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Sliding Window Aggregation in Python

MediumPython00:00
Practice interviewer
In session
5 left
00:00

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.

You need to log in / sign up to run or submit.

Problem

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.

Formal Specification

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.
  • Return a list of dictionaries in input order. Each dictionary must contain timestamp, city, ride_count, and total_fare.
  • total_fare should preserve the numeric type resulting from adding the input fares.

Constraints

  • 1 <= len(events) <= 10^5
  • 1 <= window_seconds <= 10^9
  • Timestamps are nonnegative and sorted in nondecreasing order
  • City names are nonempty strings
  • Fares are nonnegative integers or floating-point values

Function Signature

def aggregate_ride_metrics(events, window_seconds):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output