# The Top Reviewer

> One restaurant receives the most feedback - which one?

Canonical URL: <https://datadriven.io/problems/the_top_reviewer>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a non-empty dict mapping restaurant names to lists of reviews, return the name whose list is longest. If tied, return the alphabetically first name.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **dict iteration** with **max-by-value** selection. It probes whether a candidate can find the key with the maximum derived value (list length) without sorting the entire dict.

---

### Break down the requirements

#### Step 1: Iterate through the dictionary

For each restaurant, compute the number of reviews (length of the review list).

#### Step 2: Track the restaurant with the most reviews

Maintain a running maximum count and the corresponding restaurant name.

---

### The solution

**Linear scan for max review count**

```python
def most_reviewed(restaurants: dict) -> str:
    best_name = None
    best_count = -1
    for name, reviews in restaurants.items():
        review_count = len(reviews)
        if review_count > best_count:
            best_count = review_count
            best_name = name
    return best_name
```

> **Time and Space Complexity**
>
> **Time:** O(n) where n is the number of restaurants. Each `len()` call is O(1).
> 
> **Space:** O(1). Only two tracking variables are used.

> **Interviewers Watch For**
>
> Using `max(restaurants, key=lambda r: len(restaurants[r]))` is the one-liner approach. But interviewers want to see you can implement the mechanics manually.

> **Common Pitfall**
>
> Sorting the entire dict by review count is O(n log n) when only the maximum is needed. A single-pass scan is optimal.

---

## Common follow-up questions

- What if you needed the top 3 restaurants? _(Tests using a heap or sorted approach for top-k selection.)_
- What if ties should be broken alphabetically? _(Tests adding a secondary comparison on the restaurant name.)_
- What if reviews had ratings and you needed the highest average? _(Tests computing averages during iteration and comparing floats.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_top_reviewer)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.