Design a Food Rating System - Problem
๐ฝ๏ธ Food Rating System Challenge
You're tasked with building a sophisticated food rating system for a restaurant review platform! Your system needs to efficiently manage food ratings across different cuisines and provide instant access to top-rated dishes.
Your Mission:
- ๐ Update ratings - When users change their opinion about a dish
- ๐ Find top dishes - Quickly identify the highest-rated food for any cuisine type
- ๐ Handle ties - When ratings are equal, prioritize dishes alphabetically
System Requirements:
Implement the FoodRatings class with:
FoodRatings(foods[], cuisines[], ratings[])- Initialize with parallel arrayschangeRating(food, newRating)- Update a food's ratinghighestRated(cuisine)- Return the top-rated food for a cuisine
Note: For ties in ratings, return the lexicographically smaller food name (dictionary order).
Input & Output
example_1.py โ Basic Operations
$
Input:
foods = ["kimchi", "miso", "sashimi", "bibimbap"]
cuisines = ["korean", "japanese", "japanese", "korean"]
ratings = [9, 12, 8, 8]
foodRatings = FoodRatings(foods, cuisines, ratings)
print(foodRatings.highestRated("korean")) # "kimchi"
print(foodRatings.highestRated("japanese")) # "miso"
foodRatings.changeRating("sashimi", 16)
print(foodRatings.highestRated("japanese")) # "sashimi"
โบ
Output:
kimchi
miso
sashimi
๐ก Note:
Initially, kimchi (9) is highest for Korean and miso (12) for Japanese. After updating sashimi to 16, it becomes the highest-rated Japanese food.
example_2.py โ Lexicographic Tie Breaking
$
Input:
foods = ["pizza", "pasta", "risotto"]
cuisines = ["italian", "italian", "italian"]
ratings = [8, 8, 8]
foodRatings = FoodRatings(foods, cuisines, ratings)
print(foodRatings.highestRated("italian")) # "pasta"
foodRatings.changeRating("pizza", 10)
print(foodRatings.highestRated("italian")) # "pizza"
โบ
Output:
pasta
pizza
๐ก Note:
When all foods have rating 8, 'pasta' wins lexicographically (comes first alphabetically). After pizza gets rating 10, it becomes the clear winner.
example_3.py โ Multiple Rating Changes
$
Input:
foods = ["ramen", "curry", "sushi"]
cuisines = ["japanese", "indian", "japanese"]
ratings = [10, 15, 12]
foodRatings = FoodRatings(foods, cuisines, ratings)
print(foodRatings.highestRated("japanese")) # "sushi"
foodRatings.changeRating("ramen", 14)
print(foodRatings.highestRated("japanese")) # "ramen"
foodRatings.changeRating("sushi", 16)
print(foodRatings.highestRated("japanese")) # "sushi"
โบ
Output:
sushi
ramen
sushi
๐ก Note:
Initially sushi (12) > ramen (10). After ramen becomes 14, it's the highest. After sushi becomes 16, it regains the top position.
Constraints
- 1 โค n โค 2 ร 104
- 1 โค foods[i].length, cuisines[i].length โค 10
- foods[i], cuisines[i] consist of lowercase English letters
- 1 โค ratings[i] โค 108
- 1 โค newRating โค 108
- At most 2 ร 104 calls will be made to changeRating and highestRated
- All food names are unique
Visualization
Tap to expand
Understanding the Visualization
1
System Setup
Create hash maps for instant food lookups and priority queues for each cuisine's rankings
2
Rating Update
When a rating changes, update the food's info and add a new entry to the cuisine's heap
3
Best Food Query
Pop from the cuisine's heap until finding the current highest-rated food, handling outdated entries
4
Tie Breaking
Lexicographic ordering is naturally handled by the heap's comparison function
Key Takeaway
๐ฏ Key Insight: Combine hash maps for O(1) food lookups with priority queues for efficient per-cuisine rankings, handling updates and queries optimally.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code