Compute centered averages and merge intervals
Problem Description
This problem consists of two independent data manipulation tasks using Python and the pandas library.
Task 1: Centered Sliding Window Average
Given a pandas DataFrame df with columns row_id (integer) and value (float), which is guaranteed to be sorted by row_id, and an integer k (where k >= 0), you are required to compute a centered sliding window average.
Create a new column named window_avg. For each row i, window_avg should be the average of the value column for rows from i-k to i+k, inclusive. If a specific row i does not have at least k preceding rows and k succeeding rows available within the DataFrame, its window_avg should be set to -1.
Task 2: Merge Overlapping Intervals
Given a pandas DataFrame intervals with columns start (integer) and end (integer), where each row represents a time interval for an autonomous driving event and it's guaranteed that start <= end for all intervals, you are required to merge all overlapping intervals.
The output should be a new DataFrame containing non-overlapping intervals, sorted by their start times.
AI interview prep
Core dilemma
Balancing idiomatic and performant Pandas operations with precise algorithmic logic and edge case handling for both windowed calculations and interval merging.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Compute centered averages and merge intervals
Problem Description
This problem consists of two independent data manipulation tasks using Python and the pandas library.
Task 1: Centered Sliding Window Average
Given a pandas DataFrame df with columns row_id (integer) and value (float), which is guaranteed to be sorted by row_id, and an integer k (where k >= 0), you are required to compute a centered sliding window average.
Create a new column named window_avg. For each row i, window_avg should be the average of the value column for rows from i-k to i+k, inclusive. If a specific row i does not have at least k preceding rows and k succeeding rows available within the DataFrame, its window_avg should be set to -1.
Task 2: Merge Overlapping Intervals
Given a pandas DataFrame intervals with columns start (integer) and end (integer), where each row represents a time interval for an autonomous driving event and it's guaranteed that start <= end for all intervals, you are required to merge all overlapping intervals.
The output should be a new DataFrame containing non-overlapping intervals, sorted by their start times.