The Crowd Favorite Eatery
An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L3
Problem
When two restaurants tie on review count, the one whose name comes first alphabetically wins. From our review feed restaurants, which maps each restaurant name to its list of review strings, return the restaurant with the most reviews.
One restaurant clearly won the most hearts.
Example 1
Input:
restaurants = {"Dosa Place":["good","great","amazing"],"Burger Barn":["ok","meh"]}Output:
"Dosa Place"
Example 2
Input:
restaurants = {"Pho King":["fresh"],"Wok Star":["spicy","hot"],"Bao House":["yum","tasty"]}Output:
"Bao House"
Worked solution and explanation
What this really is
This is an argmax with a two-part ordering wearing a restaurant-review costume. Everyone finds the longest list; that part is free. What separates candidates is that the winner is defined by review count first and name second, and that an empty feed has a defined answer. Reach for max(restaurants, key=lambda k: len(restaurants[k])) and you hand back 'Wok Star' on the tie instead of 'Bao House', then crash with ValueError the moment the feed is empty.
Building the scan
Step 1: Count each restaurant's reviews
Walk restaurants.items() once, unpacking each pair into name and reviews, and take len(reviews) as the count. Lists know their own size, so this is constant time per restaurant.
Step 2: Seed the leader so the empty case falls out
Start best_name at "" and best_count at -1. The empty string is the answer the empty feed needs, and -1 guarantees the first real restaurant, even one with zero reviews, takes the lead.
Step 3: Let a tie go to the earlier name
A restaurant takes the lead when it has strictly more reviews, or the same count and a name that sorts earlier (name < best_name). That second condition is the whole problem: without it, whoever the dict happened to list first keeps the crown.
def max_reviews_restaurant(restaurants: dict) -> str:
best_name = ""
best_count = -1
for name, reviews in restaurants.items():
count = len(reviews)
more_reviews = count > best_count
wins_tie = count == best_count and name < best_name
if more_reviews or wins_tie:
best_count = count
best_name = name
return best_nameNaming more_reviews and wins_tie keeps the two-part ordering readable at a glance.
- Keyed max
max(restaurants, key=...)keeps the first restaurant it saw at the top count, so a tie follows insertion order, and{}raisesValueError.- Explicit scan
- The loop compares count, then name, so the tie is decided by the alphabet, and the seeded
best_name = ""falls straight out for an empty map.
Common follow-up questions
- How would you return the top three restaurants instead of one? (Tests keeping a sorted top-k without sorting the whole map.)
- What if each review carried a star rating and ties broke on average rating first? (Tests adding a second accumulator in the same pass.)
- What changes if names can differ only by casing, like 'bao house' and 'Bao House'? (Tests normalizing names before comparing them.)