Compute window averages and merge intervals
WeRideData Scientist · Technical ScreenLast updated
Compute window averages and merge intervals evaluates SQL or pandas logic, joins, grouping, window functions, null handling, edge cases, and validation in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Question
You are given two independent pandas tasks.
1. Centered sliding-window average
- Input DataFrame
dfwith columns: row_idINT — unique row-order key, already sorted ascendingvalueFLOAT — numeric value- Given an integer
k >= 0, add a new columnwindow_avgsuch that for each rowi:
window_avg(i) = average(value[i-k], ..., value[i], ..., value[i+k])
(the average of the current value plus the k values before it and the k values after it).
- Only compute the average when the row has at least
kprevious rows andklater rows. For the firstkrows and the lastkrows, setwindow_avg = -1. - Return the original DataFrame with the new column
window_avg(columnsrow_id,value,window_avg).
2. Merge autonomous-driving event intervals
- Input DataFrame of time intervals, each representing an event generated by an autonomous vehicle. Depending on the variant you are handed, the schema is one of:
vehicle_idSTRING,start_tsTIMESTAMP,end_tsTIMESTAMP, orvehicle_idSTRING,event_typeSTRING,start_tsTIMESTAMP,end_tsTIMESTAMP (group by both), or- a single table
intervalswithstartINT andendINT. - Assume all timestamps share the same timezone and
start <= endfor every row. - Within each group (per
vehicle_id, or per(vehicle_id, event_type)whenevent_typeis present — the whole table is one group when there is no grouping column), merge intervals that overlap or touch: a later interval is merged into the current one whennext.start <= current.end. - Return one row per merged interval with the group key(s) plus
merged_start/merged_end(namedmerged_start_ts/merged_end_tswhen the inputs are timestamps), sorted by the group key(s) thenmerged_start.
Write pandas code for both tasks.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.