Zomato: Top 10 Restaurants by Latest Customer Ratings (PySpark)
PYSPARK coding challenge · Difficulty: medium · Topic: Deduplication with ROW_NUMBER · +100 XP
Problem
*Zomato-style interview practice.*
Zomato wants a Top 10 restaurants list ranked by customer rating. The ratings DataFrame keeps every version of every review, so a customer who changed their mind appears more than once. Make sure each customer counts once per restaurant, with their current opinion. Use the PySpark DataFrame API (or Spark SQL).
- Customers can edit a review, and every version is kept as its own row. For each restaurant, only a customer's latest rating of that restaurant counts: the row with the latest
rated_at. If the same customer has two ratings of the same restaurant in the same second, the one with the higherrating_idis the later one. - A customer's ratings of different restaurants are separate: rating one restaurant never replaces a rating of another.
avg_ratingis the average of the ratings that count, rounded to 2 decimal places.review_countis the number of customers whose rating counts (not the number of rating rows).- Rank restaurants by
avg_ratingfrom highest to lowest. Break ties byreview_count(more customers first), then byrestaurant_id(A to Z). - Return the top 10 restaurants. If there are fewer than 10, return them all.
Assign the result to df_result with the columns restaurant_id, avg_rating and review_count, in ranking order, and finish with df_result.show().
Schema
DataFrame: ratings
| Column | Type | Meaning | | --- | --- | --- | | rating_id | integer | Unique id of this rating row; a later save gets a higher id | | restaurant_id | string | The restaurant being rated | | user_id | string | The customer who rated it | | rating | double | 1.0 to 5.0 | | rated_at | string | When this version of the review was saved, `yyyy-MM-dd HH:mm:ss` |
Example Input: `ratings`
| rating_id | restaurant_id | user_id | rating | rated_at | | --- | --- | --- | --- | --- | | 1 | R101 | U1 | 4.0 | 2025-05-01 10:00:00 | | 7 | R102 | U4 | 4.5 | 2025-05-01 13:00:00 | | 2 | R101 | U2 | 4.5 | 2025-05-01 11:30:00 | | 3 | R101 | U3 | 4.0 | 2025-05-02 09:15:00 | | 6 | R102 | U1 | 3.5 | 2025-05-01 12:00:00 | | 4 | R101 | U1 | 5.0 | 2025-05-03 09:00:00 | | 8 | R103 | U5 | 4.8 | 2025-05-02 12:00:00 | | 9 | R103 | U5 | 3.0 | 2025-05-02 12:00:00 | | 10 | R103 | U6 | 4.4 | 2025-05-02 18:45:00 | | 11 | R104 | U2 | 4.5 | 2025-05-02 20:00:00 | | 12 | R104 | U7 | 4.5 | 2025-05-03 08:30:00 | | 13 | R105 | U8 | 4.0 | 2025-05-03 10:10:00 |
Expected Output
`
+-------------+----------+------------+
|restaurant_id|avg_rating|review_count|
+-------------+----------+------------+
| R101| 4.5| 3| | R104| 4.5| 2| | R102| 4.0| 2| | R105| 4.0| 1| | R103| 3.7| 2|
+-------------+----------+------------+
`
Explanation
- R101: U1 rated it 4.0, then edited the review to 5.0 two days later, so only 5.0 counts. With U2's 4.5 and U3's 4.0 the average is (5.0 + 4.5 + 4.0) / 3 = 4.50 from 3 customers. Counting both of U1's versions would give 4.38 from 4 rows.
- R102: U1's rating of R102 still counts. Their later rating of R101 is a different restaurant: (3.5 + 4.5) / 2 = 4.00.
- R103: U5 saved two ratings at exactly 12:00:00. Rating 9 has the higher
rating_id, so it is the later one and 3.0 counts, not 4.8: (3.0 + 4.4) / 2 = 3.70. - Ties: R101 and R104 both average 4.50; R101 has more customers (3 against 2), so it ranks first. R102 and R105 both average 4.00; R102 (2 customers) comes before R105 (1).
- There are only 5 restaurants here, so all 5 are returned.
Notes
ratings,spark,F(pyspark.sql.functions) andWindoware ready. Do not recreateratings.rated_atstrings sort in time order, so you can order by them directly or convert them withF.to_timestamp.- Your code is also run on hidden datasets with more restaurants and edited reviews, so it must compute the answer from
ratings. - The row order is graded.
What this PYSPARK challenge teaches you
“Zomato: Top 10 Restaurants by Latest Customer Ratings (PySpark)” is a medium-level PYSPARK challenge focused on Deduplication with ROW_NUMBER. Working through it gives you hands-on practice with row_number, Window, Deduplication, Latest record per group, avg, Top-N with tie-breakers — 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
- row_number
- Window
- Deduplication
- Latest record per group
- avg
- Top-N with tie-breakers
- 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.row_number() over Window.partitionBy('restaurant_id', 'user_id').orderBy(rated_at desc, rating_id desc) marks each customer's latest rating.
- Filter rn == 1, then groupBy('restaurant_id') with F.round(F.avg('rating'), 2) and F.count('*').
- orderBy avg_rating desc, review_count desc, restaurant_id, then .limit(10) 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
- Deduplicate User Login Sessions
- Remove Duplicate Customer Records
- Remove Duplicate Customer Records (PySpark)
- Zomato: Top 10 Restaurants by Latest Customer Ratings
- Fix the Broken Pipeline
- Optimize the 100M-Row Join
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 Deduplication with ROW_NUMBER.