Point-in-Time Rolling User Aggregates

Quick Overview

Implement a pandas function that adds point-in-time features per user: a 7-day event count and a 30-day mean amount, using only strictly earlier events. It tests window boundaries, timestamp ties, leakage control and vectorized group computation.

Point-in-Time Rolling User Aggregates

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `build_point_in_time_features(events)`. It returns a copy of `events` with two columns appended. For each row at time `t`, use only the same `user_id`'s events with timestamp strictly before `t`: - `txn_count_7d` (`int64`): count of prior events with `timestamp >= t - 7 days`. - `mean_amount_30d` (`float64`): mean `amount` of prior events with `timestamp >= t - 30 days`. It is `NaN` when there are none. - Events sharing the exact same timestamp never count toward each other. - Keep the original row order, index, and columns. Do not mutate the input. Example with one user: ```text user_id timestamp amount -> txn_count_7d mean_amount_30d u 2024-01-01 00:00:00 4.0 -> 0 NaN u 2024-01-08 00:00:00 8.0 -> 1 4.0 # exactly 7 days back: included u 2024-01-31 00:00:00 16.0 -> 0 6.0 # exactly 30 days back: included u 2024-01-31 00:00:01 32.0 -> 1 12.0 # 01-01 is now outside 30 days ``` Example with unsorted rows, two users and a repeated timestamp: ```text user_id timestamp amount -> txn_count_7d mean_amount_30d a 2024-03-02 12:00:00 5.0 -> 1 3.0 a 2024-03-01 12:00:00 3.0 -> 0 NaN b 2024-03-01 12:00:00 7.0 -> 0 NaN a 2024-03-02 12:00:00 9.0 -> 1 3.0 # the other 03-02 row is a tie, so it is excluded ``` Both 03-02 rows of user `a` see only the 03-01 event, and user `b` has no earlier events of its own. ```hint Where each window sits Once one user's events are sorted by time, every window is a contiguous run of them. Think about how to find both ends of that run without scanning, and which end has to stop before the ties at `t`. ``` ```hint Sums over a run The sum of amounts over any contiguous run can come from two lookups into a precomputed array. ``` ### Constraints & Assumptions - `events` has columns `user_id`, `timestamp` (`datetime64`), and `amount` (float). - Rows may be unsorted, the index may be arbitrary, and timestamps may repeat within a user. - Output columns are the input columns followed by `txn_count_7d`, then `mean_amount_30d`. - Windows are exact elapsed durations, as the one-second row in the first example shows. ### Clarifying Questions to Ask - How many rows and how many distinct users should the function handle? - Can `user_id` or `timestamp` be missing, and what should those rows get? - Can `amount` be `NaN`? If so, does that event still count in `txn_count_7d`, and is it skipped in the mean? - Are the timestamps event times or ingestion times, and are they timezone-aware? ### What a Strong Answer Covers - Window bounds that match the requirements exactly: the lower bound inclusive, the upper bound strictly before `t`, and rows at the same timestamp excluded from each other. - No mixing of users, and no row using data from its own future. - A vectorized method with its time complexity stated, and how it behaves with many users. - An output that keeps row order, index, input columns and the `int64` / `float64` dtypes, with the input left unmodified. - Edge cases: empty input, a user with one event, unsorted rows, repeated timestamps. ### Follow-up Questions - How would you express this with built-in pandas rolling windows, and where do repeated timestamps cause trouble? - How do you join these features onto labels at prediction time without leakage? - How would you serve these aggregates online at low latency while keeping the training semantics?

Overview: Implement a pandas function that adds point-in-time features per user: a 7-day event count and a 30-day mean amount, using only strictly earlier events. It tests window boundaries, timestamp ties, leakage control and vectorized group computation.

PracHub logo
General
Nov 25, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement build_point_in_time_features(events). It returns a copy of events with two columns appended. For each row at time t, use only the same user_id's events with timestamp strictly before t:

  • txn_count_7d ( int64 ): count of prior events with timestamp >= t - 7 days .
  • mean_amount_30d ( float64 ): mean amount of prior events with timestamp >= t - 30 days . It is NaN when there are none.
  • Events sharing the exact same timestamp never count toward each other.
  • Keep the original row order, index, and columns. Do not mutate the input.

Example with one user:

user_id  timestamp             amount   -> txn_count_7d  mean_amount_30d
u        2024-01-01 00:00:00   4.0      -> 0             NaN
u        2024-01-08 00:00:00   8.0      -> 1             4.0    # exactly 7 days back: included
u        2024-01-31 00:00:00   16.0     -> 0             6.0    # exactly 30 days back: included
u        2024-01-31 00:00:01   32.0     -> 1             12.0   # 01-01 is now outside 30 days

Example with unsorted rows, two users and a repeated timestamp:

user_id  timestamp             amount   -> txn_count_7d  mean_amount_30d
a        2024-03-02 12:00:00   5.0      -> 1             3.0
a        2024-03-01 12:00:00   3.0      -> 0             NaN
b        2024-03-01 12:00:00   7.0      -> 0             NaN
a        2024-03-02 12:00:00   9.0      -> 1             3.0    # the other 03-02 row is a tie, so it is excluded

Both 03-02 rows of user a see only the 03-01 event, and user b has no earlier events of its own.

Constraints & Assumptions

  • events has columns user_id , timestamp ( datetime64 ), and amount (float).
  • Rows may be unsorted, the index may be arbitrary, and timestamps may repeat within a user.
  • Output columns are the input columns followed by txn_count_7d , then mean_amount_30d .
  • Windows are exact elapsed durations, as the one-second row in the first example shows.

Clarifying Questions to Ask Guidance

  • How many rows and how many distinct users should the function handle?
  • Can user_id or timestamp be missing, and what should those rows get?
  • Can amount be NaN ? If so, does that event still count in txn_count_7d , and is it skipped in the mean?
  • Are the timestamps event times or ingestion times, and are they timezone-aware?

What a Strong Answer Covers Guidance

  • Window bounds that match the requirements exactly: the lower bound inclusive, the upper bound strictly before t , and rows at the same timestamp excluded from each other.
  • No mixing of users, and no row using data from its own future.
  • A vectorized method with its time complexity stated, and how it behaves with many users.
  • An output that keeps row order, index, input columns and the int64 / float64 dtypes, with the input left unmodified.
  • Edge cases: empty input, a user with one event, unsorted rows, repeated timestamps.

Follow-up Questions Guidance

  • How would you express this with built-in pandas rolling windows, and where do repeated timestamps cause trouble?
  • How do you join these features onto labels at prediction time without leakage?
  • How would you serve these aggregates online at low latency while keeping the training semantics?