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 arrays
  • changeRating(food, newRating) - Update a food's rating
  • highestRated(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
๐Ÿฝ๏ธ Smart Food Rating SystemFood Lookupkimchi โ†’ korean, 9miso โ†’ japanese, 12sashimi โ†’ japanese, 8bibimbap โ†’ korean, 8O(1) AccessKorean Heap9kimchi8bibimbapJapanese Heap12miso8sashimiOperationschangeRating: O(1)highestRated: O(log k)Lexicographic: Auto๐ŸŽฏ Key Benefitsโœ“ Instant food lookups with hash mapsโœ“ Efficient ranking with priority queues per cuisineโœ“ Automatic lexicographic tie-breakingโœ“ Handles outdated entries gracefully๐Ÿ’ก Smart Design: Hash + Heap = Speed + SimplicityPerfect for high-frequency rating updates and queries
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.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
28.4K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen