← Weride Interview Insights

Weride·Data Scientist·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

WeRide data scientist round with two pandas problems back to back. Both were heavier than I expected for a DS role, felt more like a data engineering screen honestly.

Questions Asked (2)

Q1

Write a pandas function that adds a centered sliding-window average column to a DataFrame. For each row, average the k rows before it, the row itself, and the k rows after it. Rows without a full window on either side should get -1.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The edge case handling is where I fumbled.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the window definition and edge case behavior, then implement using pandas rolling with center=True and min_periods=2k+1, replacing NaN with -1. Discuss trade-offs between rolling and manual convolution, and mention performance considerations for large DataFrames.

Pro tip: Mention that rolling with center=True and min_periods ensures only full windows are computed, and that replacing NaN with -1 is efficient. Also note that for very large DataFrames, using numpy's sliding_window_view or convolution can be faster, but pandas rolling is more readable and handles edge cases.

1. Clarify requirements and edge cases

Confirm the window size k, the meaning of 'full window' (exactly 2k+1 rows), and that rows without a full window get -1. Ask if the DataFrame is sorted or if the window should be based on row order.

2. Choose implementation method

Decide between pandas rolling with center=True and min_periods=2k+1, or manual convolution. Discuss trade-offs: rolling is concise and handles NaN, but may be slower for large data; convolution is faster but requires more code.

3. Implement the function

Write a function that takes a DataFrame, column name, and k, computes the centered rolling mean, and fills NaN with -1. Use df[col].rolling(window=2*k+1, center=True, min_periods=2*k+1).mean().fillna(-1).

4. Test and validate

Test with small examples, including edge cases like k=0, k larger than DataFrame length, and non-numeric columns. Verify that only full windows get averages and others get -1.

5. Discuss performance and alternatives

Mention that for large DataFrames, using numpy's sliding_window_view or scipy's convolve can be more efficient. Also note that if the DataFrame is very large, out-of-core or parallel processing might be needed.

Key Points to Mention

  • Use of pandas rolling with center=True and min_periods=2k+1 to ensure only full windows are averaged.
  • Replacing NaN with -1 using fillna(-1) to handle rows without a full window.
  • Trade-offs between pandas rolling (readable, handles NaN) and manual convolution (faster for large data).
  • Edge cases: k=0 (window size 1, no -1s), k larger than DataFrame length (all -1), and non-numeric columns (should raise error or be ignored).
  • Performance considerations: rolling may be slower for very large DataFrames; alternatives like numpy's sliding_window_view or scipy's convolve can be used.
  • Assumption that the DataFrame is sorted in the order the window should be applied; if not, sorting or resetting index may be needed.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Given a DataFrame of time intervals per vehicle (and sometimes per event type), write pandas code to merge overlapping or touching intervals within each group, returning one row per merged interval.

Algorithms & Data StructuresData Modeling
Author's notes

Classic interval merge but in pandas, which is annoying.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, sort the DataFrame by group keys and interval start times. Then, within each group, identify overlapping or touching intervals by comparing the current start with the cumulative maximum end of previous intervals, and assign a new group ID whenever a break occurs. Finally, aggregate each group to get the merged start and end times.

Pro tip: Mention that using a vectorized approach with groupby and cummax is more efficient than row-wise iteration, and clarify how you handle edge cases like intervals that touch exactly (end == next start) and null values.

1. Sort and prepare data

Sort the DataFrame by the grouping columns (e.g., vehicle, event type) and the interval start time to ensure intervals are processed in order.

2. Compute cumulative max end per group

Within each group, compute the cumulative maximum of the end times up to the previous row to track the furthest end seen so far.

3. Identify new interval groups

Create a boolean flag that is True when the current start is greater than the cumulative max end (i.e., a new interval begins), then use cumsum to assign a unique group ID to each merged interval.

4. Aggregate merged intervals

Group by the original group keys and the new group ID, then compute the minimum start and maximum end to produce one row per merged interval.

5. Clean and return result

Reset the index, drop the temporary group ID column, and ensure the output has the correct columns and data types.

Key Points to Mention

  • Sorting by start time within each group is essential for the merge logic to work correctly.
  • Use of cumulative maximum (cummax) to efficiently track the furthest end without explicit loops.
  • Handling of touching intervals: decide whether end == next start should be merged (usually yes) and implement accordingly.
  • Vectorized operations with pandas groupby and transform for performance on large datasets.
  • Edge cases: empty groups, null values, and intervals that are already non-overlapping.
  • The final aggregation step uses min(start) and max(end) to produce the merged intervals.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.