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).

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

Notes

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

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:

  1. F.lag over Window.partitionBy('user_id').orderBy(<seconds>, 'event_id') gives each event the same user's previous event.
  2. F.unix_timestamp('event_time') turns the string into seconds; flag a new session when the gap is null or > 1800.
  3. F.sum('new_session') over the same window with rowsBetween(Window.unboundedPreceding, Window.currentRow) numbers the sessions.
  4. 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

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.

Solve this challenge free on PySpark.in