The Deep Dictionary
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
- L6
Problem
Find the restaurant with the most reviews in data, which maps each restaurant name to the list of individual scores it has received. Every score counts as one review whatever its value, and data always holds at least one restaurant.
One key goes further than the rest.
Example 1
Input:
data = {"Diner":[2,3,4],"Bistro":[5,5]}Output:
"Diner"
Example 2
Input:
data = {"east":[3,4],"west":[5,5],"north":[1,2]}Output:
"east"
Worked solution and explanation
What this problem really is
Beneath the restaurant framing, this asks which key has the largest value of a metric you have to compute. The metric is not stored in the dict: each value is a list of scores, and you rank by how many scores there are, len(reviews), not by what they say. Anyone can loop over the items. Candidates separate on two things: seeing that a restaurant with two glowing reviews loses to a mediocre one with three, and settling ties by name instead of by whichever entry the dict happened to list first. Rank by the sum and you return the wrong name whenever the best-scored list is not the longest. Skip the tie-break and you return the wrong name whenever the alphabetically first name is not listed first.
Break down the requirements
Step 1: Count reviews per restaurant
Each key is a name and each value is a list of review scores. The review count is the list's length; the values only tell you how good the reviews were, and that is not the question.
Step 2: Track the winner with a name tie-break
Compare each count against the running best and take the new name when its count is larger. On an equal count, take the new name only if it sorts before the current winner. The second example shows why: all three restaurants have two reviews, and 'east' wins even though 'west' is listed before it.
Step 3: Seed the best count below zero
Start the running best count at -1, not 0. Then a restaurant with an empty list still beats the starting value, and a single restaurant with no reviews is returned instead of None.
The solution
def the_deep_dictionary(data: dict[str, list[int]]) -> str:
best_name = None
best_count = -1
for name, reviews in data.items():
count = len(reviews)
if count > best_count or (count == best_count and name < best_name):
best_count = count
best_name = name
return best_nameCommon follow-up questions
- What if ties should return all winners instead of one? (Tests collecting every name that shares the maximum count.)
- What if you ranked by the average score instead of the count, and how would you treat an empty list then? (Tests ranking by a different derived metric.)
- How would you return the top `k` restaurants by review count? (Tests heap-based or sorted top-k selection.)