Zomato: Sessionize App Events and Flag Sessions That Led to an Order (PySpark)
PYSPARK coding challenge · Difficulty: medium · Topic: Sessionization · +100 XP
Problem
*Zomato-style interview practice.*
The Zomato app logs every tap in app_events, but it never records sessions. Product analysts need them: how long people browse, how many taps a visit takes, and which visits end in an order. Build the sessions from the event times with the PySpark DataFrame API (or Spark SQL).
- Work per user, in time order: by
event_time, then byevent_idfor events in the same second. - A user's first event starts session 1. A new session starts whenever the gap since that user's previous event is more than 30 minutes. A gap of exactly 30 minutes stays in the same session; 30 minutes and 1 second starts a new one.
- Number each user's sessions 1, 2, 3, ... in time order (
session_no). - For every session return
session_start(time of its first event),session_end(time of its last event),event_count(every event, including one whoseevent_typeis NULL) andordered: 1 if the session contains at least oneplace_orderevent, otherwise 0. - Another user's events never split or join a session. Sessions can cross midnight.
Assign the result to df_result with the columns user_id, session_no, session_start, session_end, event_count and ordered, sorted by user_id, then session_no, and finish with df_result.show().
Schema
DataFrame: app_events
| Column | Type | Meaning | | --- | --- | --- | | event_id | integer | Unique id of the event | | user_id | string | The user who tapped | | event_time | string | When it happened, `yyyy-MM-dd HH:mm:ss` | | event_type | string | `open`, `search`, `view_menu`, `add_to_cart`, `place_order`, or null when the app did not send one |
Example Input: `app_events`
| event_id | user_id | event_time | event_type | | --- | --- | --- | --- | | 1 | U1 | 2025-03-01 09:00:00 | open | | 8 | U2 | 2025-03-01 09:10:00 | open | | 2 | U1 | 2025-03-01 09:05:00 | search | | 3 | U1 | 2025-03-01 09:20:00 | view_menu | | 9 | U2 | 2025-03-01 09:30:00 | view_menu | | 4 | U1 | 2025-03-01 09:50:00 | add_to_cart | | 5 | U1 | 2025-03-01 09:52:00 | place_order | | 6 | U1 | 2025-03-01 10:22:01 | open | | 7 | U1 | 2025-03-01 10:25:00 | search | | 10 | U2 | 2025-03-01 11:00:00 | open | | 11 | U2 | 2025-03-01 11:10:00 | place_order | | 12 | U2 | 2025-03-01 11:15:00 | place_order | | 13 | U3 | 2025-03-01 23:50:00 | open | | 15 | U3 | 2025-03-02 00:15:00 | NULL | | 14 | U3 | 2025-03-02 00:10:00 | view_menu |
Expected Output
`
+-------+----------+-------------------+-------------------+-----------+-------+
|user_id|session_no| session_start| session_end|event_count|ordered|
+-------+----------+-------------------+-------------------+-----------+-------+
| U1| 1|2025-03-01 09:00:00|2025-03-01 09:52:00| 5| 1| | U1| 2|2025-03-01 10:22:01|2025-03-01 10:25:00| 2| 0| | U2| 1|2025-03-01 09:10:00|2025-03-01 09:30:00| 2| 0| | U2| 2|2025-03-01 11:00:00|2025-03-01 11:15:00| 3| 1| | U3| 1|2025-03-01 23:50:00|2025-03-02 00:15:00| 3| 0|
+-------+----------+-------------------+-------------------+-----------+-------+
`
Explanation
- U1: 09:20 → 09:50 is exactly 30 minutes, so the add-to-cart stays in session 1, which ends with the order at 09:52. The next event, 10:22:01, is 30 minutes and 1 second later: session 2. (Measuring the gap in whole minutes would round it down to 30 and miss this split.)
- U2: its events sit between U1's but are a separate timeline. 09:30 → 11:00 is 90 minutes, so 11:00 starts session 2, which places two orders:
orderedis still 1. - U3: 23:50 → 00:10 → 00:15 is one session across midnight. The 00:15 event has no type, but it is still an event: 3 events.
Notes
app_events,spark,F(pyspark.sql.functions) andWindoware ready. Do not recreateapp_events.- Your code is also run on hidden datasets with other users and gaps, so it must compute the answer from
app_events. - The row order is graded.
What this PYSPARK challenge teaches you
“Zomato: Sessionize App Events and Flag Sessions That Led to an Order (PySpark)” is a medium-level PYSPARK challenge focused on Sessionization. Working through it gives you hands-on practice with Sessionization, lag, Window, rowsBetween, unix_timestamp, Gaps and Islands — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- Sessionization
- lag
- Window
- rowsBetween
- unix_timestamp
- Gaps and Islands
- Food Delivery
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- F.lag over Window.partitionBy('user_id').orderBy(<seconds>, 'event_id') gives each event the same user's previous event.
- F.unix_timestamp('event_time') turns the string into seconds; flag a new session when the gap is null or > 1800.
- F.sum('new_session') over the same window with rowsBetween(Window.unboundedPreceding, Window.currentRow) numbers the sessions.
- groupBy('user_id', 'session_no'): count('*') (not count('event_type')) and max(when(...)) for ordered; orderBy before show().
Where this comes up
Variations of this problem have been reported in interviews at Zomato. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your PySpark code in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related PYSPARK challenges
- Zomato: Sessionize App Events and Flag Sessions That Led to an Order
- Fix the Broken Pipeline
- Optimize the 100M-Row Join
- Parse Apache Logs with Regex
- Optimize Small DataFrame Join with Broadcast
- Optimize Average Rating Calculation for Products
Helpful resources
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The PYSPARK environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated medium and covers Sessionization.