Compute window averages and merge intervals
Problem Description
You are presented with two independent data manipulation tasks using pandas.
Task 1: Sliding-Window Average
Given an input pandas DataFrame df with the following schema:
row_id(INT): A unique identifier for each row, guaranteed to be sorted in ascending order.value(FLOAT): The numerical value for the row.
And an integer k where k >= 0, compute for each row the average of values from the k preceding rows, the current row, and the k succeeding rows.
For any row that does not have at least k preceding rows and k succeeding rows available within the DataFrame, the window_avg for that row should be set to -1.
The function should return a new DataFrame containing the original row_id and value columns, along with a new window_avg column.
Task 2: Merge Overlapping Autonomous-Driving Intervals
Given an input pandas DataFrame segments with the following schema:
vehicle_id(STRING): Identifier for the vehicle.event_type(STRING): Type of the autonomous driving event.start_ts(TIMESTAMP): The start timestamp of the event interval.end_ts(TIMESTAMP): The end timestamp of the event interval.
Assume all timestamps are in the same timezone and that start_ts <= end_ts for every row.
For each unique combination of (vehicle_id, event_type), independently merge intervals that either overlap or touch. An interval next should be merged into current if next.start_ts <= current.end_ts.
The function should return a DataFrame with the columns vehicle_id, event_type, merged_start_ts, and merged_end_ts. The result should be sorted first by vehicle_id, then by event_type, and finally by merged_start_ts.
You are required to write pandas code to solve both tasks.
AI interview prep
Core dilemma
Efficiently implementing complex data transformations (sliding windows with specific edge cases, and grouped interval merging) using idiomatic pandas, demonstrating an understanding of both performance implications and correctness.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Compute window averages and merge intervals
Problem Description
You are presented with two independent data manipulation tasks using pandas.
Task 1: Sliding-Window Average
Given an input pandas DataFrame df with the following schema:
row_id(INT): A unique identifier for each row, guaranteed to be sorted in ascending order.value(FLOAT): The numerical value for the row.
And an integer k where k >= 0, compute for each row the average of values from the k preceding rows, the current row, and the k succeeding rows.
For any row that does not have at least k preceding rows and k succeeding rows available within the DataFrame, the window_avg for that row should be set to -1.
The function should return a new DataFrame containing the original row_id and value columns, along with a new window_avg column.
Task 2: Merge Overlapping Autonomous-Driving Intervals
Given an input pandas DataFrame segments with the following schema:
vehicle_id(STRING): Identifier for the vehicle.event_type(STRING): Type of the autonomous driving event.start_ts(TIMESTAMP): The start timestamp of the event interval.end_ts(TIMESTAMP): The end timestamp of the event interval.
Assume all timestamps are in the same timezone and that start_ts <= end_ts for every row.
For each unique combination of (vehicle_id, event_type), independently merge intervals that either overlap or touch. An interval next should be merged into current if next.start_ts <= current.end_ts.
The function should return a DataFrame with the columns vehicle_id, event_type, merged_start_ts, and merged_end_ts. The result should be sorted first by vehicle_id, then by event_type, and finally by merged_start_ts.
You are required to write pandas code to solve both tasks.