Zomato: Top 3 Dishes per Restaurant by Share of Delivered Quantity (PySpark)

PYSPARK coding challenge · Difficulty: hard · Topic: Window Functions · +150 XP

Problem

*Based on a Zomato data interview question.*

Zomato's partner dashboard shows every restaurant its top 3 dishes and what share of the restaurant's delivered quantity each one is. Using the PySpark DataFrame API (or Spark SQL), build that list for every restaurant in orders.

Assign the result to df_result with the columns restaurant_id, dish_name, total_quantity, share_pct and dish_rank, sorted by restaurant_id, then dish_rank, then dish_name, and finish with df_result.show().

Schema

DataFrame: orders

| Column | Type | Meaning |
| --- | --- | --- |
| order_id | integer | One order line |
| restaurant_id | string | The restaurant that received the order |
| dish_name | string | The dish ordered |
| quantity | integer | Units of the dish in this order (at least one) |
| status | string | `delivered` or `cancelled` (always lowercase) |

Example Input: `orders`

| order_id | restaurant_id | dish_name | quantity | status |
| --- | --- | --- | --- | --- |
| 1 | R101 | Biryani | 5 | delivered |
| 2 | R101 | Burger | 2 | delivered |
| 3 | R101 | Paneer Tikka | 4 | delivered |
| 4 | R102 | Pizza | 3 | delivered |
| 5 | R101 | Biryani | 4 | delivered |
| 6 | R101 | Pasta | 1 | delivered |
| 7 | R102 | Momos | 4 | delivered |
| 8 | R103 | Pizza | 20 | delivered |
| 9 | R101 | Burger | 5 | delivered |
| 10 | R102 | Momos | 10 | cancelled |
| 11 | R101 | Paneer Tikka | 3 | delivered |
| 12 | R102 | Noodles | 1 | delivered |
| 13 | R101 | Pasta | 4 | delivered |
| 14 | R102 | Pizza | 3 | delivered |
| 15 | R102 | Noodles | 1 | delivered |
| 16 | R101 | Dosa | 3 | delivered |
| 17 | R102 | Wrap | 3 | delivered |
| 18 | R102 | Noodles | 1 | delivered |
| 19 | R103 | Burger | 2 | delivered |
| 20 | R101 | Burger | 6 | cancelled |
| 21 | R102 | Fries | 1 | delivered |
| 22 | R104 | Thali | 7 | cancelled |
| 23 | R104 | Lassi | 2 | cancelled |

Expected Output

`

+-------------+------------+--------------+---------+---------+

|restaurant_id|   dish_name|total_quantity|share_pct|dish_rank|

+-------------+------------+--------------+---------+---------+

|         R101|     Biryani|             9|    29.03|        1|
|         R101|      Burger|             7|    22.58|        2|
|         R101|Paneer Tikka|             7|    22.58|        2|
|         R102|       Pizza|             6|    35.29|        1|
|         R102|       Momos|             4|    23.53|        2|
|         R102|     Noodles|             3|    17.65|        3|
|         R102|        Wrap|             3|    17.65|        3|
|         R103|       Pizza|            20|    90.91|        1|
|         R103|      Burger|             2|     9.09|        2|

+-------------+------------+--------------+---------+---------+

`

Explanation

Notes

What this PYSPARK challenge teaches you

“Zomato: Top 3 Dishes per Restaurant by Share of Delivered Quantity (PySpark)” is a hard-level PYSPARK challenge focused on Window Functions. Working through it gives you hands-on practice with groupBy, sum, Window, partitionBy, rank, Percentage of total — 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. Filter status == 'delivered' first, then groupBy('restaurant_id', 'dish_name') and sum the quantity.
  2. The share needs the restaurant's total next to each dish: F.sum('total_quantity').over(Window.partitionBy('restaurant_id')). Compute it BEFORE you keep the top 3, or the base shrinks.
  3. Rank inside each restaurant: F.rank().over(Window.partitionBy('restaurant_id').orderBy(F.col('total_quantity').desc())). row_number() drops ties and dense_rank() lets too many dishes in.
  4. Keep dish_rank <= 3, then orderBy('restaurant_id', 'dish_rank', 'dish_name') before show(): the order is graded.

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 hard and covers Window Functions.

Solve this challenge free on PySpark.in