The Repeat Review
A medium Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- medium
- Seniority
- L3
- Asked in
- Meta
Problem
Our reviews feed maps each store location to the phrases customers left there. Find the phrase with the widest reach: the one that turns up in the most distinct stores, counting a phrase once per store however many times that store's customers repeated it.
The echo came back.
Example 1
Input:
store_reviews = {"LA":["great","ok"],"SF":["great","fantastic"],"NYC":["great","great","ok","terrible"]}Output:
"great"
Example 2
Input:
store_reviews = {"Austin":["pricey","cozy"],"Portland":["cozy","pricey"]}Output:
"cozy"
Worked solution and explanation
Reach, not volume
This is a document-frequency count wearing a customer-reviews costume: you want how many distinct stores mention a phrase, not how many times it was typed. Anyone can loop and tally. The move that separates people is collapsing each store's reviews to a set before you count anything: skip it and a single location that pastes 'great service' fifty times buries a phrase that genuinely shows up everywhere. You are counting reach, not volume.
Break down the requirements
Step 1: Deduplicate reviews within each store
For each store, convert its reviews to a set so that repeated submissions from the same store count as one.
Step 2: Count store appearances per review
For each unique review across all stores, count how many distinct stores submitted it. This is the cross-group aggregation the trap disguises.
Step 3: Find the widest-reaching review
Return the review that appears in the highest number of distinct stores. When two reviews are level, the alphabetically smaller one wins, so keep the current best only if the challenger has a strictly higher count or is alphabetically smaller at an equal count.
The solution
def most_frequent_review(store_reviews: dict[str, list[str]]) -> str:
review_store_count = {}
for reviews in store_reviews.values():
for review in set(reviews):
review_store_count[review] = review_store_count.get(review, 0) + 1
best_review = None
best_count = 0
for review, count in review_store_count.items():
if count > best_count or (count == best_count and review < best_review):
best_count = count
best_review = review
return best_reviewset(reviews) is the whole game: it turns raw volume into distinct-store reach.
- Naive: flat count
- Concatenate every store's list and Counter it. A store repeating 'slow' four times contributes 4, so 'slow' can beat a phrase that four different stores each said once.
- Correct: per-store dedup
- set() each store's reviews first, then count stores. 'slow' from one noisy store contributes 1, and the phrase four stores actually share wins on reach.
Common follow-up questions
- What if you needed the top 3 most widespread reviews? (Tests extracting multiple items from the count dict using a heap or sorted extraction.)
- What if reviews were fuzzy (for example 'great' vs 'Great!' vs 'GREAT')? (Tests text normalization before deduplication.)
- How does this pattern relate to TF-IDF? (Tests awareness that document frequency is the same concept applied to text search.)