Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

First and Last Value

PySpark data engineering interview problem. Difficulty: intermediate. Pattern: Window Functions. About 16 minutes. Part of the Pro drill bank.

First and last event_type per user ordered by ts. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

For each user_id ordered by ts, add first_event_type and last_event_type. Return user_id, ts, event_type, and both derived columns. Order by user_id, ts. Assign result.

Requirements

  • Running first/last labels per user.

Constraints

  • Order by ts.

Examples

Input: events for user 1 Output: user_id | ts | event_type | first_event_type | last_event_type 1 | 2024-01-01 09:00:00 | view | view | view 1 | 2024-01-01 10:00:00 | purchase | view | purchase 1 | 2024-01-01 11:00:00 | purchase | view | purchase first stays at the earliest event_type; last tracks through the ordered frame.

Topics: lakebench, pyspark, first, last.

More PySpark interview questions · All interview problems · Learn data engineering

intermediate

First and Last Value

Interview-style drill: First and last event_type per user ordered by ts.

For each `user_id` ordered by `ts`, add `first_event_type` and `last_event_type`. Return `user_id`, `ts`, `event_type`, and both derived columns. Order by `user_id`, `ts`. Assign `result`.