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

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

Notes

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

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.row_number() over Window.partitionBy('restaurant_id', 'user_id').orderBy(rated_at desc, rating_id desc) marks each customer's latest rating.
  2. Filter rn == 1, then groupBy('restaurant_id') with F.round(F.avg('rating'), 2) and F.count('*').
  3. 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

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.

Solve this challenge free on PySpark.in