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: Track Best Foods by CuisineInput Foodskimchi (korean, 9)miso (japanese, 12)sushi (japanese, 8)OperationschangeRating(food, newRating)highestRated(cuisine)→ Handle ties lexicographicallyQuery ResultshighestRated("japanese")→ "miso" (rating 12)Best in cuisineSystem Challenge: Efficiently find highest-rated food per cuisineKey Requirements• Fast rating updates for any food item• Quick queries for best food in any cuisine typeGoal: O(log k) queries using hash maps + heaps
Understanding the Visualization
1
Input
Foods with cuisines and ratings
2
Operations
Support rating updates and highest-rated queries
3
Output
Return best food per cuisine (lexicographic tiebreaker)
Key Takeaway
🎯 Key Insight: Hash maps provide O(1) food access while heaps maintain sorted order per cuisine for fast queries
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