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.
- Only delivered orders count. Cancelled orders are ignored everywhere: in the totals, the shares and the ranking.
total_quantityis the sum ofquantityfor that dish at that restaurant. It is not the number of orders.share_pctistotal_quantityas a percentage of that restaurant's total delivered quantity, over all of its dishes (not just its top 3), rounded to 2 decimals.- Rank dishes within each restaurant by
total_quantity, highest first. Equal totals share a rank, and the ranks after a tie are skipped (7, 7, 5 gives 1, 1, 3). - Keep every dish ranked 3 or better. If dishes tie at 3rd place, all of them stay, so a restaurant can return more than 3 rows. A restaurant with fewer than 3 dishes returns all of them, and a restaurant with no delivered order does not appear.
- The same dish name at two restaurants is two different dishes.
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
- R101 delivers 31 units (Burger's 6-unit order was cancelled). Biryani 9 is 29.03%. Burger and Paneer Tikka tie at 7 (22.58% each) for rank 2, so Pasta (5) is rank 4 and is left out.
- R102 delivers 17 units (the 10-unit Momos order was cancelled). Noodles has the most orders, 3, but only 3 units, and ties with Wrap at rank 3: both are returned.
- R103 has only two dishes: both are returned. Its Pizza 20 is not added to R102's Pizza.
- R104 has only cancelled orders, so it does not appear.
Notes
orders,spark,F(pyspark.sql.functions) andWindoware ready. Do not recreateorders.- Your code is also run on hidden datasets with other restaurants and dishes, so it must compute the answer from
orders. - The row order is graded.
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
- groupBy
- sum
- Window
- partitionBy
- rank
- Percentage of total
- Ties
- Top-N per group
- 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:
- Filter status == 'delivered' first, then groupBy('restaurant_id', 'dish_name') and sum the quantity.
- 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.
- 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.
- 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
- Top 3 Products per Category
- Running Total Revenue
- Median Salary per Department
- Latest Order Per Customer
- 3-Day Rolling Sum of Sales
- 7-Day Rolling Purchase Amount by Customer
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.