Zomato: Sessionize App Events and Flag Sessions That Led to an Order

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

Return user_id, session_no, session_start, session_end, event_count and ordered (the grader checks the rows, not their order).

Schema

Table: app_events

| Column | Type | Meaning |
| --- | --- | --- |
| event_id | INT | Unique id of the event |
| user_id | VARCHAR | The user who tapped |
| event_time | DATETIME | When it happened (to the second) |
| event_type | VARCHAR | `open`, `search`, `view_menu`, `add_to_cart`, `place_order`, or NULL when the app did not send one |

Tables

Table: 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 SQL challenge teaches you

“Zomato: Sessionize App Events and Flag Sessions That Led to an Order” is a medium-level SQL challenge focused on Sessionization. Working through it gives you hands-on practice with Sessionization, LAG, Running SUM, Gaps and Islands, TIMESTAMPDIFF, Conditional aggregation — 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. LAG(event_time) OVER (PARTITION BY user_id ORDER BY event_time, event_id) gives each event the same user's previous event time.
  2. Flag a new session when there is no previous event or TIMESTAMPDIFF(SECOND, prev_time, event_time) > 1800. Minutes would round 30:01 down to 30.
  3. A running SUM of the flags (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) numbers the sessions 1, 2, 3 per user.
  4. GROUP BY user_id, session_no; use COUNT(*) so NULL event types count, and MAX(CASE ...) so two orders still give 1.

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 SQL query 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 SQL challenges

Helpful resources

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The SQL 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