Design a Food Rating System - Problem

Design a food rating system that supports the following operations:

  • Modify the rating of a food item in the system
  • Return the highest-rated food item for a specific cuisine type

Implement the FoodRatings class:

  • FoodRatings(String[] foods, String[] cuisines, int[] ratings) - Initializes the system with food items, their cuisines, and initial ratings
  • void changeRating(String food, int newRating) - Changes the rating of the specified food item
  • String highestRated(String cuisine) - Returns the name of the highest-rated food for the given cuisine. If there's a tie, return the lexicographically smaller name

Input & Output

Example 1 — Basic Operations
$ Input: operations = [["FoodRatings", ["kimchi", "miso", "sushi"], ["korean", "japanese", "japanese"], [9, 12, 8]], ["highestRated", "japanese"], ["changeRating", "sushi", 16], ["highestRated", "japanese"]]
Output: ["miso", "sushi"]
💡 Note: Initially miso (12) > sushi (8) for japanese cuisine. After updating sushi to 16, sushi (16) > miso (12).
Example 2 — Lexicographic Tiebreaker
$ Input: operations = [["FoodRatings", ["apple", "banana"], ["fruit", "fruit"], [5, 5]], ["highestRated", "fruit"]]
Output: ["apple"]
💡 Note: Both have rating 5, so return lexicographically smaller: apple < banana.
Example 3 — Single Item Cuisine
$ Input: operations = [["FoodRatings", ["pizza"], ["italian"], [10]], ["highestRated", "italian"]]
Output: ["pizza"]
💡 Note: Only one item in italian cuisine, so return pizza.

Constraints

  • 1 ≤ n ≤ 2×104
  • 1 ≤ foods[i].length, cuisines[i].length ≤ 10
  • 1 ≤ ratings[i] ≤ 108
  • At most 2×104 calls to changeRating and highestRated

Visualization

Tap to expand
Food Rating System Design INPUT Food Cuisine Rating kimchi korean 9 miso japanese 12 sushi japanese 8 Operations: 1. highestRated("japanese") 2. changeRating("sushi", 16) 3. highestRated("japanese") Data Structures: HashMap food-->info cuisine-->heap Priority Queue Max Heap by rating, name ALGORITHM STEPS 1 Initialize Data Store food info in HashMap Build heap per cuisine 2 highestRated Query Get cuisine's max heap Return top valid entry 3 changeRating Update food's rating Push new entry to heap 4 Lazy Deletion Skip stale heap entries Verify against HashMap Japanese Cuisine Heap: Before: miso:12 sushi:8 After: sushi:16 miso:12 FINAL RESULT Query 1: highestRated("japanese") miso: 12 sushi: 8 "miso" After: changeRating("sushi", 16) Query 2: highestRated("japanese") sushi: 16 miso: 12 "sushi" OUTPUT: ["miso", "sushi"] OK - Verified Ties: lexicographically smaller Key Insight: Use HashMap for O(1) food lookup and Max Heap (Priority Queue) per cuisine for O(log n) highest rating queries. Lazy deletion handles rating changes: push new entries, validate against HashMap when popping stale entries. TutorialsPoint - Design a Food Rating System | Hash Map + Priority Queue Approach
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
28.4K Views
Medium Frequency
~25 min Avg. Time
892 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